使用 log4net 创建可以使用 SvcTraceViewer.exe 查看的日志文件
使用具有正确格式的 log4net 记录到文件的最佳方法是什么(正确的 XML、正确的时间戳格式、正确格式的自定义数据、正确的属性,基本上与 XmlWriterTraceListener 会执行此操作),以便可以在 服务跟踪查看器工具 (SvcTraceViewer.exe)?
What is the best way to log to a file using log4net that has the correct format (correct XML, correct timestamp format, custom data in correct format, correct attributes, basically the exact same way as XmlWriterTraceListener does it) so it can be viewed in the Service Trace Viewer Tool (SvcTraceViewer.exe)?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果我想这样做,那么我会编写我的自定义布局。我(还)没有查看详细信息,但我会编写一个派生自
XmlLayoutBase
的类。我需要更多时间来查看细节...您也可以编写自己的附加程序,但我认为在这种情况下编写布局类更有意义。
编辑:也许编写自己的附加程序是个好主意。在这种情况下,您可以使用 System.ServiceModel.Diagnostics.DiagnosticTrace 类。但还不确定这是否是正确的方法。我现在没有太多时间,但我会研究一下。
If I wanted to this then I would write my custom layout. I did not (yet) look at the details but I would write a class that derives from
XmlLayoutBase
. I need some more time to look at the details...You could also write your own appender but I think in this case it makes more sense to write a layout class.
Edit: Maybe writing your own appender is a good idea. In that case you could use the
System.ServiceModel.Diagnostics.DiagnosticTrace
class. Not sure yet though if that is the way to go. I do not have much time right now, but I will look into this.不是答案,但我今天早些时候问了一个关于日志记录和 WCF 的问题,以及我想要的东西之一要知道的是关于服务跟踪查看器。我见过的所有示例都描述了通过 System.Diagnostics TraceSources 和 System.Diagnostics XmlFileListener 生成的 Service Trace Viewer 使用的 XML 文件。不管怎样,如果我在我的帖子中得到任何答案,你可能会发现它们很有用。
Not an answer, but I asked a question earlier today about logging and WCF and one of the things I wanted to know was about Service Trace Viewer. All of the examples that I have seen describe the XML files consumed by Service Trace Viewer being generated via System.Diagnostics TraceSources and the System.Diagnostics XmlFileListener. Anyway, if I get any answers in my post you might find them useful.
这里有一个想法:
您可以编写一个自定义 log4net Appender 并让它将消息(间接)写入 XmlWriterTraceListener。在 Append 方法中,您只需将消息发送到 System.Diagnostics。
以下是一个自定义 Appender 的示例。
在示例中,Append 被覆盖。它传递一个 LoggingEvent 类/结构。出于您的目的(为了将 log4net 输出路由到 SvcTraceViewer 可以读取的输出格式),您可以将输出写入 System.Diagnostics(首先将其配置为记录到 XmlWriterTraceListener)。您可以使用 Trace.Write* 方法、Trace.Trace* 方法或通过 TraceSources 进行写入。
对于 TraceSources,您可以认为 TraceSource 名称与记录器名称相同(在 LoggingEvent 类/结构中可用)。因此,您可以在 app.config 文件中为要进入 xml 文件的每个 log4net 记录器名称配置 TraceSource。您的追加逻辑可能如下所示:
这可能会给您带来您想要的结果。请注意,我实际上还没有这样做,所以我不能说这是否是一个好主意,但它看起来确实可行。
很抱歉说得简短,但我想在离开之前完成这件事。
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:
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.