TraceOutputOptions doesn't work

Topics: Logging Application Block
Jul 2, 2014 at 3:16 AM
Hi All,

The following is my configuration, as you can see, I set TraceOutputOptions value as Datetime and StackTrace, but the Log file I got doesn't work as my expectation. Normally, If I get an exception, the log file should output stacktrace, while I didn't get the log.
Is there anyone, who can help me?


<loggingConfiguration name="LoggerConfig" tracingEnabled="true" defaultCategory="Trace">
<listeners>
  <add name="FileListener" type="Microsoft.Practices.EnterpriseLibrary.Logging.TraceListeners.FlatFileTraceListener, Microsoft.Practices.EnterpriseLibrary.Logging, Version=6.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
    listenerDataType="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.FlatFileTraceListenerData, Microsoft.Practices.EnterpriseLibrary.Logging, Version=6.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
    fileName="trace.log" formatter="TextFormatter" traceOutputOptions="DateTime, Callstack" />
</listeners>
<formatters>
  <add type="Microsoft.Practices.EnterpriseLibrary.Logging.Formatters.TextFormatter, Microsoft.Practices.EnterpriseLibrary.Logging, Version=6.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
    template="Timestamp: {timestamp}{newline}&#xA;Message: {message}{newline}&#xA;Category: {category}{newline}&#xA;Priority: {priority}{newline}&#xA;EventId: {eventid}{newline}&#xA;Severity: {severity}{newline}&#xA;Title:{title}{newline}&#xA;Machine: {localMachine}{newline}&#xA;App Domain: {localAppDomain}{newline}&#xA;ProcessId: {localProcessId}{newline}&#xA;Process Name: {localProcessName}{newline}&#xA;Thread Name: {threadName}{newline}&#xA;Win32 ThreadId:{win32ThreadId}{newline}&#xA;Extended Properties: {dictionary({key} - {value}{newline})}"
    name="TextFormatter" />
</formatters>
<categorySources>
  <add switchValue="All" name="Trace">
    <listeners>
      <add name="FileListener" />
    </listeners>
  </add>
</categorySources>
<specialSources>
  <allEvents switchValue="All" name="All Events" />
  <notProcessed switchValue="All" name="Unprocessed Category" />
  <errors switchValue="All" name="Logging Errors &amp; Warnings">
    <listeners>
      <add name="FileListener" />
    </listeners>
  </errors>
</specialSources>

</loggingConfiguration>

Timestamp: 7/2/2014 1:55:54 AM
Message: Manually Exception!
Category: Trace
Priority: 1
EventId: 0
Severity: Information
Title:
Machine:
App Domain:
ProcessId: 21988
Process Name:
Thread Name:
Win32 ThreadId:21812

Extended Properties:

Jul 2, 2014 at 9:26 AM
If you remove the formatter, "TextFormatter", then you should see the properties appear. It looks like activity tracing is handled by the System.Diagnostics.TraceListener base class (and not by Enterprise Library) and the appropriate method will only be called when the Formatter is set to null.

~~
Randy Levy
entlib.support@live.com
Enterprise Library support engineer
Support How-to
Marked as answer by vincent_wu01 on 7/2/2014 at 8:01 PM
Jul 3, 2014 at 4:10 AM
Hi Randy,

That's what I need, I've been searched for this issue for a long time, and didn't get any answer online, now I finally get what I want, thanks a lot, that's awesome!
There's another question I want to ask, after I set text formatter as none, the output text still print the text formatter things, like the following paragraphs:

Message: Manually Exception!
Category: Trace
Priority: 1
EventId: 0
Severity: Information
Title:
Machine:
App Domain:

I don't know why, could you please explain this for me? Thanks in advance.
Jul 6, 2014 at 6:52 PM
What you are seeing is the default formatter that is used in the LogEntry.ToString() override. The LogEntry is passed in to System.Diagnostics.TraceListener.TraceData as the data argument and the TraceData implemention will call ToString() on the (LogEntry) object passed in. Even if you are not explicitly using a LogEntry one is created for you when calling Write().

If you wanted to change that text you could create a custom LogEntry object that overrides ToString() and implement your own ToString() formatting:
    public class MyLogEntry : LogEntry
    {
        public override string ToString()
        {
            return String.Empty;
        }
    }

    LogEntry logEntry = new MyLogEntry() 
    { 
        Categories = new string[] { "Trace" },
        Message = "Exception"
    };

    logger.Write(logEntry);

~~
Randy Levy
entlib.support@live.com
Enterprise Library support engineer
Support How-to
Jul 9, 2014 at 4:39 AM
I see, this is good way to override other format, thanks a lot, this rules out my doubts.