Trace to different logfiles from the same application

546 views Asked by At

First some background information about the situation. I have a main application (let's call it the Core) and some minor applications (let's call them Core jobs). The main job of the Core is to make sure, that every any given Core job is running according to the schedule of the said job. It's all developed in .NET C#

I would like to make an environment where each Core job can trace information to different logfiles. I considered to use simple filestreams, but I believe there must be a more elegant solution to this.

It seems to me, that I might be able to use the inbuilt TraceSource and listeners. I made a TextWriterTraceListener for each job and started tracing. The result: Every job traced information to every listener.

Back to the drawing board. I studied it a bit more and now can see how foolish my first approach was. I now made a source for each job and a listener for each source. In the code I specify which source I like to trace to. But with little success :(

Snippet from app.config:

  <system.diagnostics>
    <switches>
      <add name="AMSSUY" value="All"/>
    </switches>
    <sources>
      <source name="AMSSUY_TraceSource">
        <listeners>
          <add name="AMSSUY_Listener" type="System.Diagnostics.TextWriterTraceListener"
               initializeData="ksltest.log"/>
        </listeners>
      </source>
    </sources>
  </system.diagnostics>

Snippet from code:

private static TraceSource log = new TraceSource("AMSSUY_TraceSource");
        log.TraceEvent(TraceEventType.Information, 1, "test test test");

I've tried to make shared listeners as well and it didn't help. Not really surprising, because as far as I can tell, the purpose of shared listeners are for more sources to share the same listener.

On some forums people keep talking about log4net - can that solve my problem? I have absolutely no experience with it.

What I would like to know is: Are there an elegant solution to my problem? Is it possible to solve my problem using tracesources and listeners?

2

There are 2 answers

3
paxvinci On

Have you ever tried log4net? It is simple to use and stable: http://logging.apache.org/log4net/ There are a lot of example of how to customize log and allow multiple format, see http://logging.apache.org/log4net/release/config-examples.html

0
ngeksyo On

You can use Nlog as an alternative to log4net.

You can immediately get it implemented with minimal configuration and is actively on continuous development.