locked
How can i limit logging in my app using the web.config ? RRS feed

  • Question

  • User1253338400 posted

    Hi ,

    In my app I have logging , but I would like to limit what I log. So foir example if I have a setting in web.config to log the rrrors or information.

    In my web.config file I have the following:

    <system.diagnostics>
    <trace autoflush="true"/>
    <sources>
    <source name="MyLogFileSource" switchValue="Error">
    <listeners>
    <remove name="Default"/>
    <add name="MyLogFileListener"
          type="System.Diagnostics.TextWriterTraceListener"
         initializeData="iMyLogFileOrder.log"
         traceOutputOptions="DateTime">
    </add>
    </listeners>
    </source>

    I call a number of statements in my applications 

    So for example 

    try{

         ... some code 

         TraceSource LogFile = new TraceSource("MyLogFileSource", SourceLevels.All);

         LogFile.TraceInformation("TEST TO LOG ONLY ERRORS THIS SHOULD NOT BE LOGGED");

       .. some more code runs and exception happens here. So it falls through to the catch .

    }

    Catch Exception (ex) {

         LogFile.TraceEvent(TraceEventType.Error, 0, "Error MyLogFileSource: ");

         LogFile.TraceData(TraceEventType.Error, 0, ErrorMessage(ex));

    }

    Since the web.config is configured to error , I would have thought only the error would be logged and not the Information ,

    "TEST TO LOG ONLY ERRORS THIS SHOULD NOT BE LOGGED"

    Is the Web config configured correctly for the trace type ?

    Would it log an exception regardless of the setting ?

    Thanks

    Thursday, June 1, 2017 6:24 AM

Answers

  • User1967761114 posted

    Hi robby32,

    According to your description, you want to trace more event types by TraceSource, in configuration, you could set switchValue by use comma(,) to separate different types.

    See the following configuration, I had been tested, it works well.

    <system.diagnostics>
      <trace autoflush="true"/>
      <sources>
        <source name="MyLogFileSource" switchValue="Error,Information" >
          <listeners>
            <remove name="Default"/>
            <add name="MyLogFileListener"
                  type="System.Diagnostics.TextWriterTraceListener"
                 initializeData="iMyLogFileOrder.log"
                 traceOutputOptions="DateTime">
            </add>
          </listeners>
        </source>
      </sources>
    

    All the trace event types could be used in swithValue, you could refer to the following link:

    https://msdn.microsoft.com/en-us/library/system.diagnostics.traceeventtype(v=vs.110).aspx

    If you have any other questions, please feel free to contact me any time.

    Best Regards

    Even

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, June 2, 2017 5:07 AM