System.Diagnostics Tracks the template for the source name.

I have my Loggingset up to use a different one TraceSourcefor everyone Class.

Is it possible to configure a wildcard that records 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>
+5
source share
1 answer

I do not know how to do this automatically. However, if you look at TraceLogger in the Castle git repository, you will see that they have essentially a wrapped and extended TraceSource to support hierarchy naming.

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

I would copy the code here, but it may not be right to just cut and paste the code there in SO.

, , , ( Castle)

, ( ) "" ( TraceSource). , , . , . TraceSource, , TraceSource . , . . TraceSource, , . . TraceSources, "". TraceSource, ( "). TraceSource, , " TraceSource ", " *" TraceSource .

, - :

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.

}

- ( ), , , log4net NLog.

!

+4

All Articles