C-va
C-va

Reputation: 2910

TraceSource : Custom TraceListener not working

I'm using TraceSource to log the ASP.Net application. To change the output format I need to create own TraceListener instead of TextwriterListener.

   <trace>
        <listeners>
            <remove name="Default" />
             <add name="Default"
             type="WebTracing.CustomTraceListener, WebTracing" initializeData="Weblog.txt" traceOutputOptions="None"/>
        </listeners>
    </trace>

 public class CustomTraceListener : TextWriterTraceListener
 {
    public override void Write(string message)
    {
        if (NeedIndent)
        {
            WriteIndent();
        }
        message = //customize format
        base.Write(message); 
    }

    public override void WriteLine(string message)
    {
        if (NeedIndent)
        {
            WriteIndent();
        }
        message = //customize format
        base.WriteLine(message);
        NeedIndent = true;
    }
 }

// Invoke

        CustomTraceListener obj = new CustomTraceListener();
        obj.TraceEvent(null, "This is a critical message", TraceEventType.Critical, 0);
        (or)
         obj.Write( "This is a critical message");
        obj.Close();

No Error. No data printed in output file. I'm new to ASP.Net , Can anyone help me to solve this issue.

update


I tried another way. But results in Configuration error.

<trace autoflush="true" />
<sources>
  <source name="Trace" switchName="mySwitch" switchType="System.Diagnostics.SourceSwitch">    
      <listeners>
        <remove name="Default" />
        <add name="Default"
             type="WebTracing.CustomTraceListener, WebTracing" initializeData="Weblog.txt" traceOutputOptions="None"/>
      </listeners>      
  </source>
</sources>
<switches>
  <add name="mySwitch" value="All"/>
</switches>


        TraceSource obj = new TraceSource("Trace");
        obj.TraceEvent(TraceEventType.Critical,0,"This is a critical message");
        obj.Close();

Exception Details: System.Configuration.ConfigurationErrorsException: Could not create WebTracing.CustomTraceListener, WebTracing.

Note: Normal TextWriterListener is working fine. But i need custom listener to format the output string.

Upvotes: 3

Views: 2127

Answers (5)

Anna Dolbina
Anna Dolbina

Reputation: 1110

There is a mismatch between the configuration and implementation.

If you configure your logger as

<listeners>
        <remove name="Default" />
         <add name="Default"
         type="WebTracing.CustomTraceListener, WebTracing" initializeData="Weblog.txt"/>
</listeners>

your custom implementation should have a matching public constructor:

public CustomTraceListener(string initializeData)
{
    //configure your listener using the initializeData value
}

In other case, the ConfigurationErrorsException will be thrown.

Upvotes: 0

Nɪsʜᴀɴᴛʜ ॐ
Nɪsʜᴀɴᴛʜ ॐ

Reputation: 2904

I had faced this scenario, after adding the below snippets of code after <source> tag, it worked writing to the text file as mentioned

<switches>
      <add name="mySwitch" value="Verbose" />
</switches>

Here is the the partial snippets from Web.config file

<system.diagnostics>
  <trace autoflush="true"/>
  <sources>
    <source name="myTraceSource" switchName="mySwitch" switchType="System.Diagnostics.SourceSwitch" >
      <listeners>
        <clear/>
        <add name="textwriterListener" type="System.Diagnostics.TextWriterTraceListener"
          initializeData="d:\outfile1.txt" traceOutputOptions="ProcessId, DateTime, Callstack" />
      </listeners>
    </source>
  </sources>
  <switches>
    <add name="mySwitch" value="Verbose" />
  </switches>
</system.diagnostics>

Further, I had invoking TraceSource inside some method defined by creating a static object

static TraceSource ts = new TraceSource("myTraceSource");
...

ts.TraceEvent(TraceEventType.Critical,0,"This is a critical message"); 

Upvotes: 0

Anton Lyhin
Anton Lyhin

Reputation: 1945

Just change your relative path initializeData="Weblog.txt" to absolute initializeData="d:\Weblog.txt". I am still trying to detect the root reason why creation of custom-listner log files does not work for us.

I guess this might be because of not configured project folder permissions: SO link.


I have reproduced the same issue today and I see that your application is configured in the right way. It took me several hours to understand that the proper configuration is not sufficient condition for creating log files using custom-listeners.

When I execute TextWriterTraceListener the log is created in the project's folder. But CustomTextWriterTraceListener does not create anything.


Upvotes: 0

MatthewMartin
MatthewMartin

Reputation: 33143

You don't write directly to the Trace Listener. In this case, the System.Diagnostic.Trace object writes to the Trace Listener. If you were using TraceSources, then you'd write to the trace source and the config file let's the TraceSource know which listeners are register for receiving trace info.

Also, you are writing to a file, which usually has a flush interval, so you might want to make sure that it is autoflushing, or in your tests, write enough data to trigger a flush (when the data is written to file).

Upvotes: 0

Aghilas Yakoub
Aghilas Yakoub

Reputation: 28970

I suggest you to add public modifier to your class

And you can use

System.Diagnostics.Trace.Write method

Upvotes: 0

Related Questions