views:

520

answers:

6
+1  Q: 

File logger in C#

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.

+9  A: 

I suggest you use the open source log4net

Jhonny D. Cano -Leftware-
+2  A: 

Nlog and Log4Net are two widely used framework.

Here is a related stack overflow question.

J.W.
+7  A: 

Enterprise Library Logging Application Block.

John Saunders
A hearty recommendation for Enterprise Library.
Randolpho
A: 

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

PSU_Kardi
A: 

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");
   }
}
Mike J
+2  A: 

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.

tobsen
That's interesting, I've not seen that before, so + 1. Whether I'd use that over log4net is questionable though. I've never had the need to change my logging implementation from log4net. I guess it is an example of an abstraction over something that is fairly abstract itself!
RichardOD
When it comes to logging, personal taste does play a huge role, imo (who hasn't found himself writing a loggerclass sometime in his/her developer carrer?). I think an abstraction as commonlogging is handy when a project/team isn't clear about logging requirements or if different software components from various source are used. in the latter case bridged logging allows for cleaner logfiles and easier logmaintenance. But you are right RichardOD, after all log4net is pretty much complete ;-)
tobsen