Error logging in C#

Viewed 91980

I am making my switch from coding in C++ to C#. I need to replace my C++ error logging/reporting macro system with something similar in C#.

In my C++ source I can write

LOGERR("Some error"); or LOGERR("Error with inputs %s and %d", stringvar, intvar);

The macro & supporting library code then passes the (possibly varargs) formatted message into a database along with the source file, source line, user name, and time. The same data is also stuffed into a data structure for later reporting to the user.

Does anybody have C# code snippets or pointers to examples that do this basic error reporting/logging?

Edit: At the time I asked this question I was really new to .NET and was unaware of System.Diagnostics.Trace. System.Diagnostics.Trace was what I needed at that time. Since then I have used log4net on projects where the logging requirements were larger and more complex. Just edit that 500 line XML configuration file and log4net will do everything you will ever need :)

15 Answers

Lots of log4net advocates here so I'm sure this will be ignored, but I'll add my own preference:

System.Diagnostics.Trace

This includes listeners that listen for your Trace() methods, and then write to a log file/output window/event log, ones in the framework that are included are DefaultTraceListener, TextWriterTraceListener and the EventLogTraceListener. It allows you to specify levels (Warning,Error,Info) and categories.

Trace class on MSDN
Writing to the Event Log in a Web Application
UdpTraceListener - write log4net compatible XML messages to a log viewer such as log2console

I would highly recommend looking at log4Net. This post covers the majority of what you need to get started.

Another good logging library is NLog, which can log to a lot of different places, such as files, databases, event logger etc.

Enterprise Library is a solid alternative to log4net and it offers a bunch of other capabilities as well (caching, exception handling, validation, etc...). I use it on just about every project I build.

Highly recommended.

Even though I personally hate it, log4net seems to be the de facto standard for C# logging. Sample usage:

log4net.ILog log = log4net.LogManager.GetLogger(typeof(Program));
log.Error(“Some error”);
log.ErrorFormat("Error with inputs {0} and {1}", stringvar, intvar);

Log4Net is a rather comprehensive logging framework that will allow you to log to different levels (Debug, Error, Fatal) and output these log statements to may different places (rolling file, web service, windows errors)

I am able to easily log anywhere by creating an instance of the logger

private static readonly ILog _log = LogManager.GetLogger(typeof([Class Name]));

and then logging the error.

_log.Error("Error messsage", ex);

You can use built in .NET logging. Look into TraceSource and TraceListeners, they can be configured in the .config file.

Ditto for log4net. I'm adding my two bits because for actual use, it makes sense to look at some open source implementations to see real world code samples with some handy additions. For log4net, I'd suggest off the top of my head looking at subtext. Particularly take a look at the application start and assemblyinfo bits.

Further to the couple of comments realting to the use of the System.Diagnostics methods for logging, I would also like to point out that the DebugView tool is very neat for checking debug output when needed - unless you require it, there is no need for the apps to produce a log file, you just launch DebugView as and when needed.

Related