2

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?

Peter Duniho
  • 68,759
  • 7
  • 102
  • 136

2 Answers2

0

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

  • You're thinking along the same lines as me but I think you need to give an example configuration to solve the OP's question - otherwise he's just exchanging one technology for another without a fix – Liath Jan 23 '14 at 10:46
  • You are spot on Liath. Just now I'm looking around on the homesite of Log4Net, but as of yet, I have found nothing to make me believe that an exchange of technology would fix the issue :) – Kasper Lethan Jan 23 '14 at 10:54
  • @KasperLethan you can do it by creating two different (probably file) appenders and log to them under different circumstances (eg different namespaces/severities)... I can't go diving into config files right now but I'll see if I get the chance to later – Liath Jan 23 '14 at 10:56
0

You can use Nlog as an alternative to log4net.

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

ngeksyo
  • 419
  • 3
  • 10