Reputation: 6557
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:
Any suggestions to a way of logging that can do this and still be simple?
Upvotes: 0
Views: 137
Reputation: 70379
If you want something built-in and configurable from config-file see http://msdn.microsoft.com/en-us/library/ms228993.aspx
Upvotes: 0
Reputation: 7160
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).
Upvotes: 1
Reputation: 172
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
Upvotes: 4
Reputation: 62265
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.
Upvotes: 3
Reputation: 24293
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.
Upvotes: 2
Reputation: 24634
Log4net is a simple framework that you can utilize.
http://logging.apache.org/log4net/
Upvotes: 4