简体   繁体   English

System.Diagnostics跟踪通配符以获取源名称

[英]System.Diagnostics Traces wildcard for source name

I have my Logging set up to use a different TraceSource for each Class . 我将Logging设置为每个Class使用不同的TraceSource

Is it possible to Configure a wildcard that writes events for all Sources? 是否可以配置为所有源写入事件的通配符?

<system.diagnostics>
  <sources>
    <source name ="wildcard" switchValue="Warning">
      <listeners>
        <add name="textlog" />
      </listeners>
    </source>
    <source name="MySpecificClass" switchValue="All">
      <listeners>
        <add name="textlog" />
      </listeners>
    </source>
  </sources>
  <sharedListeners>
    <add name="textlog"
         type="System.Diagnostics.TextWriterTraceListener"
         initializeData="Log.log">
    </add>
  </sharedListeners> 
  <trace autoflush="true"/>
</system.diagnostics>

I'm not aware of a built in way to do that automatically. 我不知道有一种内置的方法可以自动完成。 However, if you have a look at the TraceLogger in Castle's git repository, you can see that they have essentially wrapped and extended TraceSource to support "hierarichal" naming. 但是,如果您查看Castle的git存储库中的TraceLogger,您可以看到它们基本上包装并扩展了TraceSource以支持“hierarichal”命名。

https://github.com/castleproject/Core/blob/master/src/Castle.Core/Core/Logging/TraceLogger.cs https://github.com/castleproject/Core/blob/master/src/Castle.Core/Core/Logging/TraceLogger.cs

I would copy the code here, but it might not be proper to just cut and paste there code into SO. 我会在这里复制代码,但是将代码剪切并粘贴到SO中可能不合适。

I can explain how the ideas presented in the class could work for your (without you having to use Castle) 我可以解释一下课堂上提出的想法如何适合你(不必使用Castle)

In essence, in your client code (that wants to log stuff), you would create an instance of your "logger" (rather than TraceSource). 实质上,在您的客户端代码(想要记录内容)中,您将创建“logger”的实例(而不是TraceSource)。 As input to the logger, you would give, for example, the fully qualified class name. 作为记录器的输入,您可以提供完全限定的类名。 Inside the constructor, use the input name to try to resolve a TraceSource. 在构造函数内部,使用输入名称尝试解析TraceSource。 If there is a TraceSource configured with that name, use that TraceSource to do the work. 如果存在使用该名称配置的TraceSource,请使用该TraceSource执行此操作。 If not, trim off the rightmost part of the fully qualified name. 如果没有,请修剪完全限定名称的最右边部分。 Try to resolve a TraceSource with that name. 尝试使用该名称解析TraceSource。 If there is a TraceSource configured with that name, use it. 如果存在使用该名称配置的TraceSource,请使用它。 And so on. 等等。 If you don't find any TraceSources, then don't log anything from your "logger". 如果找不到任何TraceSource,则不要记录“logger”中的任何内容。 You could add on the ability to recognize a TraceSource that has been configured with a wildcard name (" "). 您可以添加识别已使用通配符名称(“ ”)配置的TraceSource的功能 If you never find a TraceSource using the name trimming technique and if there is a " " TraceSource, use the "*" TraceSource as the fallback. 如果您从未使用名称修剪技术找到TraceSource,并且如果有“ ”TraceSource,请使用“*”TraceSource作为后备。

So, you might have something like this: 所以,你可能有这样的事情:

class MyTraceSource
{
  private TraceSource ts;

  public MyTraceSource(string name)
  {
    ResolveTraceSource(name);
  }

  private void ResolveTraceSource(string name)
  {
    //Check for a configured TraceSource from most qualified name (as input) to least qualified ("").
    //Assume name like this:  Namespace1:Namespace2:Class
    //Try to resolve:
    // TraceSource("Namespace1.Namespace2.Class");
    // TraceSource("Namespace1.Namespace2");
    // TraceSource("Namespace1");
    //If you still haven't found one, try to resolve
    // TraceSource("*");
  }

  //Implement either TraceSource API, or whatever API you prefer for logging.

}

I have actually done something like this myself as part of a prototype (that we ended up not using) and it worked pretty well for mimicking the way that you can specify loggers in log4net and NLog. 我实际上已经做过这样的事情作为原型的一部分(我们最终没有使用),并且它非常适合模仿在log4net和NLog中指定记录器的方式。

Good luck! 祝好运!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM