kjv
kjv

Reputation: 11327

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.

Upvotes: 4

Views: 9665

Answers (8)

Yigael Oscar
Yigael Oscar

Reputation: 21

Enterprise Library - it's open source and from Microsoft.

Upvotes: 0

Nate
Nate

Reputation: 13242

I wrote a simple .net logger with publicly available source. It may fit your needs.

Simple to use, thread safe, file and line date and time stamps, automatic file creation, parses exceptions into text for you, uses a queue to write out log messages in a background thread.

(Go to the file menu and select download to get the zip files)

Upvotes: 2

tobsen
tobsen

Reputation: 5398

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.

Upvotes: 4

Mike J
Mike J

Reputation: 3149

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");
   }
}

Upvotes: 2

ist_lion
ist_lion

Reputation: 3209

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

Upvotes: 1

John Saunders
John Saunders

Reputation: 161773

Enterprise Library Logging Application Block.

Upvotes: 9

J.W.
J.W.

Reputation: 18181

Nlog and Log4Net are two widely used framework.

Here is a related stack overflow question.

Upvotes: 5

Jhonny D. Cano -Leftware-
Jhonny D. Cano -Leftware-

Reputation: 18013

I suggest you use the open source log4net

Upvotes: 11

Related Questions