user3479806
user3479806

Reputation: 53

write log to file instead of console in postsharp

I know this question may be ridiculous but I could not find the answer. The Post sharp writes the logs in console by System.Diagnostics but I need to write the logs in a separate file. Is there any way to do so?

Thanks in advance

Upvotes: 1

Views: 332

Answers (2)

user3479806
user3479806

Reputation: 53

I found also I can do this in the app.config as the following:

<system.diagnostics>
    <trace autoflush="true" indentsize="4">
      <listeners>
        <add name="myListener" type="System.Diagnostics.TextWriterTraceListener" initializeData="C:\\log.txt" />
        <remove name="Default" />
      </listeners>
    </trace>
  </system.diagnostics>

Upvotes: 2

Daniel Balas
Daniel Balas

Reputation: 1850

You need to use System.Diagnostics.Trace.Listeners property to register your own listener. You would need code like this in your app's entry point:

using (StreamWriter sw = new StreamWriter("file.txt"))
using (TextWriterTraceListener tl = new TextWriterTraceListener(sw))
{
    Trace.Listeners.Add(tl);

    try
    {
        // execute your program here
    }
    finally
    {
        Trace.Listeners.Remove(tl);
    }
}

Upvotes: 0

Related Questions