5

使用具有正确格式的 log4net 记录到文件的最佳方式是什么(正确的 XML、正确的时间戳格式、正确格式的自定义数据、正确的属性,基本上与XmlWriterTraceListener的方式完全相同),因此可以在服务跟踪查看器工具 (SvcTraceViewer.exe ) ?

4

3 回答 3

2

如果我想这样做,那么我会编写我的自定义布局。我(还)没有查看细节,但我会编写一个派生自XmlLayoutBase. 我需要一些时间来看看细节......

您也可以编写自己的附加程序,但我认为在这种情况下编写布局类更有意义。

编辑:也许编写自己的附加程序是个好主意。在这种情况下,您可以使用System.ServiceModel.Diagnostics.DiagnosticTrace该类。还不确定这是否是要走的路。我现在没有太多时间,但我会研究一下。

于 2010-09-15T13:56:59.953 回答
1

不是答案,但我今天早些时候问了一个关于日志记录和 WCF 的问题,我想知道的一件事是关于 Service Trace Viewer。我看到的所有示例都描述了服务跟踪查看器使用的 XML 文件,这些文件是通过 System.Diagnostics TraceSources 和 System.Diagnostics XmlFileListener 生成的。无论如何,如果我在帖子中得到任何答案,您可能会发现它们很有用。

于 2010-09-09T18:19:47.620 回答
0

Here is an idea:

You could write a custom log4net Appender and have it write messages (indirectly) to the XmlWriterTraceListener. Inside the Append method you simply send the message to System.Diagnostics.

Here is one example of a custom Appender.

In the example, Append is overridden. It is passed a LoggingEvent class/structure. For your purposes (to get the log4net output routed to an output format that can be read by SvcTraceViewer), you could write your output to System.Diagnostics (having first configured it to log to the XmlWriterTraceListener). You could either write using Trace.Write* methods, Trace.Trace* methods, or by TraceSources.

For TraceSources, you could consider the TraceSource name to be the same as the logger name (which is available in the LoggingEvent class/structure). So, you could configure a TraceSource in your app.config file for each log4net logger name that you want to go into the xml file. Your Append logic then might look something like this:

protected override void Append(LoggingEvent le)
{
  TraceSource ts = new TraceSource(le.LoggerName); // Not sure of logger name field in LoggingEvent
  ts.TraceEvent(LogLevelToTraceEventType(le.Level), 0, le.FormattedMessage);
}

This might give you what you want. Note that I have not actually done this, so I cannot say if it is a good idea or not, but it certainly seems like it would work.

Sorry for being brief, but am trying to finish this before have to leave.

于 2010-09-15T18:57:43.087 回答