0

I have a rather large program that have some odd behaviour once in a while. After it has been deployed at a customer it's not possible to do debugging. But it is permissible to use log files, so this is what I have created. Something like this:

TextWriter tw = new StreamWriter(@"C:\AS-log.txt", true);
tw.WriteLine("ValidateMetaData");
tw.Close();

3 lines like this has been inserted into the code at many places and do give excellent log information. There are 2 problems with this approach however:

  1. The code looks very messy when there are more lines regarding logging than actual code.
  2. I would like to be able to switch logging on and off via a configuration file.

Any suggestions to a way of logging that can do this and still be simple?

Kasper Hansen
  • 6,307
  • 21
  • 70
  • 106
  • 3
    Have you considered using a well-regarded logging library such as [log4net](http://logging.apache.org/log4net/) or [NLog](http://nlog-project.org/)? – Jon Jul 25 '11 at 12:01
  • 1
    Duplicate...http://stackoverflow.com/questions/185542/net-logging-framework – Neville Kuyt Jul 25 '11 at 12:03
  • @Jon he is probbaly not aware of those libraries, not correct to downvote for that reason, if that was a reason. – Tigran Jul 25 '11 at 12:06

7 Answers7

4

Log4net is a simple framework that you can utilize.

http://logging.apache.org/log4net/

jishi
  • 24,126
  • 6
  • 49
  • 75
4

Maybe you could try Enterprise libraries from Microsoft. It has a logging application block which works quite nice

http://msdn.microsoft.com/en-us/library/ff648951.aspx

Wombat
  • 172
  • 3
  • 10
3

I would suggets to use log4Net. It has a huge potential, that you probbaly don't need, but give you easy predefined formatting in loog entries.

Before use it yuo should configure it your application's .config file.

This is just an example how to do, you can use others that easily can find on internet:

<log4net debug="true">
    <appender name="RollingLogFileAppender" type="log4net.Appender.RollingFileAppender">
      <file value="Logs\\TestLog.txt" />
      <appendToFile value="true" />
      <rollingStyle value="Date" />
      <maxSizeRollBackups value="10" />
      <maximumFileSize value="10MB" />
      <staticLogFileName value="false" />
      <layout type="log4net.Layout.PatternLayout">
        <conversionPattern value="%-5p %d %5rms %-22.22c{1} %-18.18M - %m%n" />
      </layout>
    </appender>

    <root>
      <level value="DEBUG" />
      <appender-ref ref="RollingLogFileAppender" />
    </root>
  </log4net>

Regards.

Tigran
  • 61,654
  • 8
  • 86
  • 123
2

You can use a Listener of System.Diagnostics.Debug and System.Diagnostics.Trace. Normally these are controlled by compile options but you can attach the listener depending on your config option.

System.Diagnosics.Trace.WriteLine("ValidateMetaData");

This also allows you to watch live with DebugView, etc.

Deanna
  • 23,876
  • 7
  • 71
  • 156
1

A simple solution would be to make the log writing a class. On application startup it opens the file for writing to, and there is a simple method Write. You can then simply use Log.Write("ValidateMetaData") which reduces the amount of code that you use inline, and stops you having to always open and close the file. You can also add checks depending on configuration (the easiest way to do that would be with application settings).

Matt
  • 7,100
  • 3
  • 28
  • 58
0

Try log4net or nlog (I prefer nlog)

Stuart
  • 66,722
  • 7
  • 114
  • 165
0

If you want something built-in and configurable from config-file see http://msdn.microsoft.com/en-us/library/ms228993.aspx

Yahia
  • 69,653
  • 9
  • 115
  • 144