了解跟踪是如何工作在C# [英] Understanding how Trace works in C#

查看:127
本文介绍了了解跟踪是如何工作在C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想了解如何跟踪作品



我创建了一个简单的新的Web项目。这是我的代码,我可以使用



  //创建事件日志跟踪侦听器。 
EventLogTraceListener myTraceListener =新的EventLogTraceListener(myEventLogSource);

//添加事件日志跟踪侦听到集合。
Trace.Listeners.Add(myTraceListener);

//输出写入事件日志。
Trace.WriteLine(测试输出);



我以帮助,从这个的



在我的web.config中的设置如下:

 <&System.Diagnostics程序GT; 
<跟踪自动冲洗=假indentsize =4>
<&听众GT;
<添加名称=myListener的
型=System.Diagnostics.EventLogTraceListener
initializeData =TraceListenerLog/>
< /听众>
< /跟踪>
< /system.diagnostics>



然而,当我运行此代码,我不知道这个记录正在发生的事情我检查事件查看器,在应用程序和服务日志我期待一些新的日志,名为myEventLogSource,但并没有发生创建。



请任何人都可以解释我如何其工作原理。


解决方案

在,如果你有两个来源即是书面方式向应用程序日志应用程序日志检查,一个是TraceListenerLog和另一种是myEventLogSource。新的日志不会被创建,他们都将使用应用程序日志。
。如果你想创建一个新的日志,你想跟踪输出写,你可以做这样的(当然,登录名不必等于源名称):

 字符串LOGSOURCE =_myEventLogSource; 
如果
EventLog.CreateEventSource(LOGSOURCE,LOGSOURCE)(EventLog.SourceExists(LOGSOURCE)!);

EventLogTraceListener myTraceListener =新的EventLogTraceListener(LOGSOURCE);

//添加事件日志跟踪侦听到集合。
System.Diagnostics.Trace.Listeners.Add(myTraceListener);

//输出写入事件日志。
System.Diagnostics.Trace.WriteLine(测试输出);



即使源就不会存在,跟踪信息将被写入到下的应用程序事件日志记录与你传递给EventLogTraceListener构造一个源名称。


I am trying to understand how does Tracing works

I have created a simple new web project. This is my code that I can using

// Create a trace listener for the event log.
EventLogTraceListener myTraceListener = new EventLogTraceListener("myEventLogSource");

// Add the event log trace listener to the collection.
Trace.Listeners.Add(myTraceListener);

// Write output to the event log.
Trace.WriteLine("Test output");

I am taking help from this msdn link

The settings in my web.config is as follows

 <system.diagnostics>
 <trace autoflush="false" indentsize="4">
  <listeners>
    <add name="myListener"
      type="System.Diagnostics.EventLogTraceListener"
      initializeData="TraceListenerLog" />
  </listeners>
 </trace>
</system.diagnostics>

However when I run this code, I dont know where this logging is happening I check the EVENT VIEWER, under the "Application and Services Log" I was expecting some new log to be created with the name "myEventLogSource" but that has not happened.

Please can anyone explain me how this works.

解决方案

Under the Application log check if you have two Sources that were writting to the Application log, one is TraceListenerLog and the other one is myEventLogSource. New log is not going to be created, they will both use Application log. If you want to create a new log and you want to write trace output to it, you can do it like this (of course, log name doesn't have to be equal to source name):

        string logSource = "_myEventLogSource";
        if (!EventLog.SourceExists(logSource))
            EventLog.CreateEventSource(logSource, logSource);

        EventLogTraceListener myTraceListener = new EventLogTraceListener(logSource);

        // Add the event log trace listener to the collection.
        System.Diagnostics.Trace.Listeners.Add(myTraceListener);

        // Write output to the event log.
        System.Diagnostics.Trace.WriteLine("Test output");

Even if the source wouldn't exist, trace information would get written to the event log under the Application log with a source name you have passed to the EventLogTraceListener constructor.

这篇关于了解跟踪是如何工作在C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆