Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/265.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 了解跟踪如何在C中工作#_C#_Trace - Fatal编程技术网

C# 了解跟踪如何在C中工作#

C# 了解跟踪如何在C中工作#,c#,trace,C#,Trace,我试图了解跟踪是如何工作的 我创建了一个简单的新web项目。这是我可以使用的代码 // 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(

我试图了解跟踪是如何工作的

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

// 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");
我从这里得到帮助

my web.config中的设置如下所示

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

但是,当我运行这段代码时,我不知道日志记录发生在哪里。我检查了事件查看器,在“应用程序和服务日志”下,我希望创建一些名为“myEventLogSource”的新日志,但这并没有发生


请任何人解释一下这是如何工作的。

在应用程序日志下检查是否有两个正在写入应用程序日志的源,一个是TraceListenerLog,另一个是myEventLogSource。不会创建新日志,它们都将使用应用程序日志。 如果您想创建一个新日志,并想向其写入跟踪输出,可以这样做(当然,日志名不必等于源名称):


即使源不存在,跟踪信息也会被写入到应用程序日志下的事件日志中,其中包含您已传递给EventLogTraceListener构造函数的源名称。

您能告诉我们在配置中将
autoflush
更改为
true
时会发生什么吗?或者
Trace
类的
AutoFlush
属性。或者,在写入后调用
Flush
。此外,XML配置使您无需在代码中创建/配置侦听器。创建新的事件日志源需要管理员权限。首先在管理命令提示符下运行此命令
eventcreate/ID 1/L APPLICATION/T INFORMATION/SO myEventLogSource/D“Created myEventLogSource”
我将尝试此操作,但如果事件源不存在,那么代码(问题中)是否会抛出错误?它将检查源是否存在(EventLog.SourceExists)(logName)),如果它不存在,将创建它(EventLog.CreateEventSource(logName,logName))。尝试一下,如果您有一个web应用程序,您可以将此代码放入,例如,Page_Load event handler。再次运行此打开的事件查看器以获取现有日志的刷新列表后。即使源不存在,跟踪信息也会写入到应用程序日志下的事件日志中,其中包含您已传递给t的源名称他是一个事件跟踪的构造函数。
        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");