I’m writing a WinForms application. I need to log information into a file. Usually I use log4net for logging, but I cannot add the reference, due to a restriction. I cannot add external references to my project, because I must deploy a single executable.
Is there any built-in logging framework in .NET so I will be able to log into a file without adding an external dll?
P.S: Of course I don’t wanna open a stream and write manually.
Yes, the System.Diagnostics.TraceListener class. You will need to define the TRACE constant for it to work, but you can use a number of built in tracelisteners, through configuration of your app.config:
The app.config looks something like this if you want to write to a file, there are a lot of filters you can also add:
And usage:
Personally I wouldn’t write to a text file if you can avoid it, the Event Log is a better location as you can sort, filter, the logs are auto-purged and you don’t get file lock up issues.