Turbojohn

Hi,

Does anyone know a good and simple way to write to an error log

For example, if an exception occurs in my program, a suitable message is written to an error log:

try

{

// blah, blah

}

catch (NullReferenceException nfex)

{

// Write to error log here

}

I have looked at a few examples, however none seem to really explain the process clearly!

Any help would be much appreciated.

Cheers,

Turbojohn



Re: Visual C# General Writing Exceptions to an Error log

ArranNyc

You could write to an event log. You can see a simple example of its use here:

http://msdn2.microsoft.com/en-us/library/system.diagnostics.eventlog.aspx

Alternatively, you could write to a plain old text file.





Re: Visual C# General Writing Exceptions to an Error log

RizwanSharp

You can use Log4Net (Open source logging component) to see a lot of possibilities of logging. If you need something specific then you can go for Event Log and custom log files.

I hope this will help.

Best Regards,

Rizwan aka RizwanSharp






Re: Visual C# General Writing Exceptions to an Error log

Turbojohn

Thanks guys!



Re: Visual C# General Writing Exceptions to an Error log

Peter Ritchie

Or, you can use Trace.Write[Line] and change your app.config to write to any number of destinations (to file, EventLog, custom, etc.). See the TraceListener class.




Re: Visual C# General Writing Exceptions to an Error log

OmegaMan

If you want to log solely using the Console.Writeline to one or more log files. I have written up about a process entitled .Net Logging: Write Once Log Many. That way if you are writing items to the screen via Console.WriteLine, it allows one to piggy back off of that and write to multiple TextWriter objects defined by the user.

Also as an aside, check out my blog entitled Exception Handling Quick Read which takes an exception and parses it out for either console output or messagebox. It categorizes the different fields and spaces out the items appropriately.





Re: Visual C# General Writing Exceptions to an Error log

Peter Ritchie

OmegaMan wrote:
If you want to log solely using the Console.Writeline to one or more log files. I have written up about a process entitled .Net Logging: Write Once Log Many. That way if you are writing items to the screen via Console.WriteLine, it allows one to piggy back off of that and write to multiple TextWriter objects defined by the user.

Also as an aside, check out my blog entitled Exception Handling Quick Read which takes an exception and parses it out for either console output or messagebox. It categorizes the different fields and spaces out the items appropriately.
What if you're not a console application That's what Trace and TraceListener's are for. The user can re-use existing TraceListener-derived classes like FileLogTraceListener, EventLogTraceListener, TextWriterTraceListenter (and if you're in ASP.NET WebPageTraceListener). You just have to use Trace instead of Console and all other changes (unless you need a customer TraceListener class) are all done in the app.config no need to create a new TextWriter class and nothing to do for initialization (your "Usage" method on your blog...) so they don't have to recompile the application if they want to output to a different/new output.

For example, the only code you need is:

Trace.WriteLine("someText");

If you want to log that to a file, change your config as follows:

Code Snippet
< xml version="1.0" encoding="utf-8" >
<configuration>
<system.diagnostics>
<trace autoflush="true" indentsize="4">
<listeners>
<add name="myFileListener" type="System.Diagnostics.TextWriterTraceListener" initializeData="TextWriterOutput.log" />
</listeners>
</trace>
</system.diagnostics>
</configuration>

If you want to log to a file and to the event log, change the config as follows:

< xml version="1.0" encoding="utf-8" >
<configuration>
<system.diagnostics>
<trace autoflush="true" indentsize="4">
<listeners>
<add name="myFileListener" type="System.Diagnostics.TextWriterTraceListener" initializeData="TextWriterOutput.log" />
<add name="myEventListener" type="System.Diagnostics.EventLogTraceListener" initializeData="mySource" />
</listeners>
</trace>
</system.diagnostics>
</configuration>