4

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.

kjv
  • 11,047
  • 34
  • 101
  • 140

8 Answers8

11

I suggest you use the open source log4net

Jhonny D. Cano -Leftware-
  • 17,663
  • 14
  • 81
  • 103
9

Enterprise Library Logging Application Block.

John Saunders
  • 160,644
  • 26
  • 247
  • 397
5

Nlog and Log4Net are two widely used framework.

Here is a related stack overflow question.

Community
  • 1
  • 1
J.W.
  • 17,991
  • 7
  • 43
  • 76
4

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
  • 5,328
  • 3
  • 34
  • 51
  • 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 Jul 11 '09 at 20:56
  • 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 Jul 11 '09 at 21:23
2

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
  • 3,049
  • 22
  • 21
2

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)

Nate
  • 12,963
  • 4
  • 59
  • 80
  • 1
    link doesn't work, goes to generic Dyn dns page :(. Found a cached copy on google: http://webcache.googleusercontent.com/search?q=cache:zGuLt5RqR7YJ:blog.nateperry.org/%3Fp%3D655+ but it doesn't have the source or download links :( – John Nov 16 '11 at 19:54
  • I cancelled my hosting plan and forgot to update the .org domain reference. The .com is still live so I updated the link. Thanks for pointing that out. – Nate Nov 17 '11 at 15:57
  • 1
    cool thanks :), btw you might want to rename the title of your site to .com vs .org :). Also the download links are "broken" in that they aren't links but just text – John Nov 17 '11 at 20:46
  • I started with the .org domain and want to keep it. I'll fix it so the .org points to the blog soon. Hope the downloads are useful for you either way. – Nate Nov 18 '11 at 21:08
  • 1
    Oh no worries just a heads up, also the download links seem to timeout so I wasn't able to get them to work yesterday. – John Nov 19 '11 at 18:09
  • link broken! where is the fix? – nawfal Feb 02 '13 at 07:00
1

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

ist_lion
  • 3,149
  • 9
  • 43
  • 73
0

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

andr
  • 15,970
  • 10
  • 45
  • 59
Yigael Oscar
  • 21
  • 1
  • 3