I am writing a WinForms application. I need to log the information in a file. I usually log in for the logging I can not add references due to restriction, I can not add context to External for my project because I have to deploy an executable.
Is there an underlying logging structure in .NET? Can I log in a file without adding an external DLL? PS: Of course I do not want to open a stream and write manually. Yes, square. You will need to define the trace consistently for this to work, but you can use a number of tracelisteners created through the setting of your app.config: If you want to write a file then app.config looks something like this, You can also add: and use: Personally I can not write in a text file if you can avoid it, the event log is a better location as you can sort, filter, log auto clean And you do not have any problem to lock the don file.
& lt ;? Xml version = "1.0" encoding = "UTF-8"? & Gt; & Lt; Configuration & gt; & Lt; System.diagnostics & gt; & Lt; Trace autoflush = "false" indentsize = "4" & gt; & Lt; Listeners & gt; & Lt; Add name = "yourName" type = "System.Diagnostics.TextWriterTraceListener" initializeData = "c: \ mylogfile.txt" /> & Lt; / Listeners & gt; & Lt; / Trace & gt; & Lt; /system.diagnostics> & Lt; / Configuration & gt;
trace .res error ("An error is Captain: {0}", e); Trace.TraceWarning ("broke the system but do not worry."); Tracetransinformation ("start the engine.");
Comments
Post a Comment