C# log component
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,
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.
Simple googling will probably help you here.
Googling for "c# log" gives lots of hits, including: http://www.thescripts.com/forum/thread442882.html
And of course, there's also your other thread here: http://forums.microsoft.com/MSDN/Sho...44812&SiteID=1
If you were more specific, perhaps "c# log file multithreading -xml", I'm sure you could find what you wanted.
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.