Is there some class that implements logging facility, that I can use?
I need to use logger in my application. I looking for some class in .net framework that i can use for this purpose - and i did not found.
Is there some ready library in .net framewor开发者_如何学Ck that i can use for organize simple log ?
When you were looking in the framework, did you not see or not like the System.Diagnostics.Trace [1] framework?
As already mentioned log4net [2] is a good framework, we use it extensively. You may also be interested in the Log facility in the Castle Windsor IoC framework [3].
[1] http://msdn.microsoft.com/en-us/library/system.diagnostics.trace.aspx
[2] http://logging.apache.org/log4net/
[3] http://www.castleproject.org/container/facilities/trunk/logging/index.html
There are many third-party libraries for this. Check out log4net for instance.
But if all you want is to write some lines to a file it may be enough to create a text file that you write to. In that case you can use System.IO.File.CreateText(filename)
to get a StreamWriter
that you can log to. Then use StreamWriter.WriteLine
to write to the log. Finally you should Close the StreamWriter
when you are done logging.
Add a TraceListener to your app.config and use System.Diagnostics.Trace.WriteLine to write to the log.
You can have separate listeners for writing to a text file, event log etc.
elmah is good option, please check
and log4net is another http://www.codeproject.com/KB/aspnet/Log4Net_Configuration.aspx
NLog or log4net are the most popular ones. I have mostly worked with NLog and are pretty satisfied with it. These logging frameworks allow you to define different rules and targets for logging, changing these on runtime (for debugging an app in production environment, for example) as well as using reflection so that your log messages can include things like the name of the class the log is originating from.
精彩评论