Thread: C# log component

  1. #1
    Registered User
    Join Date
    May 2006

    C# log component

    Hello everyone,

    I am using C# and Visual Studio 2005 to develop a class library. I am wondering whether there are any built-in log component in C# so that I can utilize directly other than write from scratch?

    I am also wondering if there does exist such log component, if multiple processes using the built-in log component to open the same log file to write log (in my application, I want all processes which loads the class library DLL to have a common log file), will there be any risk of racing condition (e.g. interlacing log of one process and another process)? Do we need any lock/synchronization approach?

    thanks in advance,
    Last edited by George2; 09-15-2007 at 03:04 AM.

  2. #2
    pwns nooblars
    Join Date
    Oct 2005
    Portland, Or
    Most times in languages if you try and open a file that you can't for some reason, you get an error code back (possibly) and you get a null handle to the file. So assuming you keep trying until you get to open the file and write to it, there should be any problem other than the slight delay it may cause (assuming you are writing data in the order of a couple lines and not a couple megs per write) I don't know if C# has a logging class built in, but I wouldn't be surprised if it did.

    I don't code in C# so I may be wrong and the rule may be different in the File IO aspect, so if you have problems, you may need to wait for a real C# programmer or debug it yourself.

  3. #3
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Simple googling will probably help you here.

    Googling for "c# log" gives lots of hits, including:

    And of course, there's also your other thread here:

    If you were more specific, perhaps "c# log file multithreading -xml", I'm sure you could find what you wanted.

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell

    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ:

    My website:
    Projects: codeform, xuni, atlantis, nort, etc.

  4. #4
    Registered User
    Join Date
    Sep 2007
    Adelaide, Australia
    Hi George,

    There are libraries such as log4net, or even the Logging Application Block from Microsoft, but both of those are really designed for enterprise logging needs, and by the time you learn them and are able to configure them, you could have shipped your application

    My preferred option is to stick with the Trace class, which you'll find in the System.Diagnostics namespace. You can create TraceListeners, which are classes that can take the log messages and store them - for example, to a file, or to the Windows event log.

    This article on MSDN gives a good example:

    As far as using the "same log" for multiple applications goes, especially if they will be running simultaneously, I would discourage using the file system for that. As you mentioned, the risk of locked files or concurrency issues is pretty high. Also, the location of log files tends not to be very obvious to users. I would suggest that for this kind of scenario, the Event Log is a much better location to write to.



Popular pages Recent additions subscribe to a feed

Similar Threads

  1. STA component call MTA component
    By George2 in forum Windows Programming
    Replies: 0
    Last Post: 03-30-2008, 07:36 AM
  2. searching problem
    By DaMenge in forum C Programming
    Replies: 9
    Last Post: 09-12-2005, 01:04 AM
  3. fprintf works in one function but not in the other.
    By smegly in forum C Programming
    Replies: 11
    Last Post: 05-25-2004, 03:30 PM
  4. log and restore
    By TopJo in forum C Programming
    Replies: 3
    Last Post: 07-01-2003, 07:14 PM
  5. My log file player; Hit the brick wall
    By Twig in forum C Programming
    Replies: 6
    Last Post: 07-27-2002, 05:35 PM