开发者

WCF tracing & message logging - trace level warning

Assume I have a config file which looks like this: ...

<system.diagnostics>
  <sources>
    <source name="System.ServiceModel" switchValue="Warning,ActivityTracing" propagateActivity="true">
      <listeners>
        <add name="ServiceModelTraceListener" />
      </listeners>
    </source>
    <source name="System.ServiceModel.MessageLogging">
      <listeners>
        <add name="ServiceModelTraceListener" />
      </listeners>
    </source>
  </sources>
  <sharedListeners>
    <add initializeData="LogServer.svclog" type="System.Diagnostics.XmlWriterTraceListener" name="ServiceModelTraceListener" />
  </sharedListeners>
  <trace autoflush="true" />
</system.diagnostics>

When using this config file every activity the caller performs against the service and each corresponding message that's sent to the service will be logged in the svclog file. Everything fine so far.

If I modify the 3rd line from the above listing to <source name="System.ServiceModel" switchValue="Warning" propagateActivity="true"> (the ActivityTracing is removed) then only those activities are logged that are at least labeled level warning. But it's still every message logged...

So is there a way to only log those message that correspond to t开发者_开发知识库hose activities that are at least warnings? Those messages that succeeded aren't very interesting in that moment, but those messages that belong to the unsuccessful activities are!


Edit To filter messages beyond the options below you may want to look into writing your own TraceSource.

Below is one I am using for a project. You could easily customize it to filter out the messages you want or perhaps hide activity if it is not in DEBUG, etc.

class DB : TraceSource
{
    public DB(string name) : base(name)
    { 
    }

    public DB(string name, SourceLevels sourceLevels) : base (name, sourceLevels)
    { 
    } 

    public void Log(object value)
    {
        WriteLine(value);
    }

    public void Error(object value)
    {
        WriteLine(value, TraceEventType.Error);
    }

    public void Error(RecordingResponseData errorResponse)
    {
        string errorMessage = "[Error] Code: "+errorResponse.ErrorCode +" Message: "+errorResponse.ErrorMessage;
        WriteLine(errorMessage, TraceEventType.Error);
    }

    public void Warn(object value)
    {
        WriteLine(value, TraceEventType.Warning);
    }

    public void WriteLine(object value, TraceEventType type = TraceEventType.Information)
    { 
        TraceEvent(type, 0, value.ToString());
    }


}

Original

Your options are:

  1. Critical
  2. Error
  3. Warning
  4. Information
  5. ActivityTracing
  6. Verbose
  7. All

Or a combination there of. If you have it set to Warning but are still getting too many messages then you may want to try Error or Critical.

ref: https://msdn.microsoft.com/en-us/library/ms733025%28v=vs.110%29.aspx?f=255&MSPPError=-2147217396


There's a switchValue available for the System.ServiceModel.MessageLogging trace switch as well. Just add that attribe to that source element and set it to Warning also and you will only see messages logged that are related to warnings.


Get rid of System.ServiceModel.MessageLogging source to get rid of logging messages to resolve "still every message logged".

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜