Try our conversational search powered by Generative AI!

Make EPiServer.Logging.TraceSourceLoggerFactory (and its logger) support asp.net trace source configuration

Vote:
 

The TraceSourceLoggerFactory does nto support asp.net configuration (and will actually break configuration if used). This is because the ctor for EPiServer.Logging.Internal.TraceSourceLogger overwrites the .net configuration settings without any way to prevent it.

This happens because the ctor for TraceSourceLogger will always overwrite the sourceSwitch setting by setting the Source.Switch property of the Logger with a new TraceSource that has the same name as the logger, with this line (decompiled):

this.Source.Switch =new SourceSwitch(this._name, defaultSwitchValue);

The full ctor looks like this:

internal TraceSourceLogger(string name, string defaultSwitchValue)
{
   this._name = name;
   this.Source =new TraceSource(name);
   this.Source.Switch =new SourceSwitch(this._name, defaultSwitchValue);
   this.MakeUniqueIDs();
}

So it doesnt matter whats set in config, it will always be overwritten, and will make things like using one common switch for multiple sources impossible.

A way to fix this would be to check if the switch is already set, and then avoid overwriting it. This can be done with the below code (and im sure in other ways too):

internal TraceSourceLogger(string name, string defaultSwitchValue)
{
   this._name = name;
   this.Source =new TraceSource(name);
   // this extra check will keep any configured trace source instead of overwriting it with one that have the same name as the trace source
   if(this.Source.Listeners.Count == 1 && this.Source.Listeners[0]is DefaultTraceListener &&this.Source.Listeners[0].Name =="Default") {
       this.Source.Switch =new SourceSwitch(this._name, defaultSwitchValue);
   }
   this.MakeUniqueIDs();
}
#187156
Jan 15, 2018 9:31
This thread is locked and should be used for reference only.
* You are NOT allowed to include any hyperlinks in the post because your account hasn't associated to your company. User profile should be updated.