Winforms Logging Structure

I am writing a WinForms application. I need to write information to a file. I usually use log4net for logging, but I can’t add the link because of the restriction. I cannot add external links to my project because I have to deploy one executable.

Is there a built -in logging structure in .NET, so I can enter the file without adding an external dll?

PS: Of course, I do not want to open the stream and write manually.

+6
source share
4 answers

Yes, the System.Diagnostics.TraceListener class. You will need to define the TRACE constant for it to work, but you can use several built-in tracelisteners using the configuration of your app.config:

app.config looks something like this, if you want to write to a file, there are many filters that you can also add:

<?xml version="1.0" encoding="utf-8"?> <configuration> <system.diagnostics> <trace autoflush="false" indentsize="4"> <listeners> <add name="yourName" type="System.Diagnostics.TextWriterTraceListener" initializeData="c:\mylogfile.txt" /> </listeners> </trace> </system.diagnostics> </configuration> 

And use:

 Trace.TraceError("There been an error captain: {0}", e); Trace.TraceWarning("The system broke but don't worry."); Trace.TraceInformation("Starting up the engines."); 

Personally, I would not write to a text file, if you can avoid it, the event log is better located, because you can sort, filter, the logs are automatically cleared, and you do not get problems with blocking files.

+11
source

Well, if you are comfortable with log4net, so be it. Use the logging tool of your choice, and use post-build ILMerge , to unite all the dependencies in a single executable file.

This is exactly the scenario for which it is intended.

There is also a GUI if you do not want to use it on the command line.

+6
source

You can use system trace calls, but this is a poor substitute for a proper logging structure such as log4net.

http://msdn.microsoft.com/en-us/library/system.diagnostics.trace.aspx

An alternative and perhaps more flexible long-term approach would be to use ILMerge to merge all of your external DLLs into a single executable file after you compile everything.

http://www.microsoft.com/en-us/download/details.aspx?id=17630

Or pack everything as an MSI or clickonce package, of course, depending on your definition of "single executable".

+2
source

The following page shows an example of manual logging in an XML file. It looks simple enough.

C # Best way to create a log file

Hope this helps.

0
source

Source: https://habr.com/ru/post/920104/


All Articles