I am looking for a .net class to deal with logging various information to a file. The logger should have timestamps, categories for the logged data (to be able to differentiate between notificiation and errors), severity levels for errors, to be able to split the log file after it exceeds a certain size.
Nlog and Log4Net are two widely used framework.
Here is a related stack overflow question.
As pervious posters have suggested, I would say take a look into Log4Net - it's similar to Log4J and allows for a lot of functionality...
I would suggest reading : http://logging.apache.org/log4net/release/faq.html
One of the simplest methods would be to employ the logs file classes found in the .NET namely, the the EventLog class (found in System.Diagnostics) that lets you access or customize Windows event logs.
Following is a usage example:
using System.Diagnostics;
class LogSample{
public static void Main()
{
// let's create our application log file
if (!EventLog.SourceExists("ApplicationLog"))
{
EventLog.CreateEventSource("ApplicationLog", "SampleLog");
}
EventLog log = new EventLog();
log.Source = "ApplicationLog";
// Here we can write the "categories" you require
log.WriteEntry("Some error entry goes here", EventLogEntryType.Error);
log.Close();
// where EventLogEntryType enum has "Error", "Warning", "Information"
// we are done with the event log ... forever (ie. we don't want it on the machine)
log.Delete("ApplicationLog");
}
}
There are various logging frameworks out there as others already said. I suggest you use CommonLogging (by Mark Pollack , Erich Eichinger , Bruno Baia - who are also the heads behind Spring.Net) so you are independant of a specific logger implementation and can change it via configuration once you find out you need another loggingfeature. .Net CommonLogging features adapters for the following Logging libraries:
- System.Trace
- Log4Net
- NLog
- MS Enterprise Library
- A simple ConsoleOut logger
and you can easily write your own adapter or bridge between the logging adapters.