使用具有正确格式的 log4net 记录到文件的最佳方式是什么(正确的 XML、正确的时间戳格式、正确格式的自定义数据、正确的属性,基本上与XmlWriterTraceListener的方式完全相同),因此可以在服务跟踪查看器工具 (SvcTraceViewer.exe ) ?
3 回答
如果我想这样做,那么我会编写我的自定义布局。我(还)没有查看细节,但我会编写一个派生自XmlLayoutBase
. 我需要一些时间来看看细节......
您也可以编写自己的附加程序,但我认为在这种情况下编写布局类更有意义。
编辑:也许编写自己的附加程序是个好主意。在这种情况下,您可以使用System.ServiceModel.Diagnostics.DiagnosticTrace
该类。还不确定这是否是要走的路。我现在没有太多时间,但我会研究一下。
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.