I've created a small C# winforms application, as an added feature I was considering adding some form of error logging into it. Anyone have any suggestions for good ways to go about this? This is a feature I've never looked into adding to previous projects, so I'm open to suggestions from Developers who have more experience.
I was considering something along the lines of writing exceptions to a specified text file, or possibly a database table. This is an application that will be in use for a few months and then discarded when a larger product is finished.
Error logs are a useful method for collecting all error data generated by an application; this often includes trapped errors that you may not need to or care to show to the end user.
Errors are divided into the following three categories: predicted, recoverable errors. predicted, unrecoverable errors. unpredicted errors.
The error-logging facility records hardware and software failures in the error log for information purposes or for fault detection and corrective action.
logging.error ('Error occurred ' + str(e)) As seen in the above code, first we need to import the logging Python library and then initialize the logger with the log file name and logging level. There are five logging levels: DEBUG, INFO, WARNING, ERROR, and CRITICAL.
What’s worse by using logging methods in the wrong way you are turning logs into garbage. Some libraries log errors using a method like error (message) and it produces entry with your log with that message, timestamp and log level. Some have separate method for logging exception object such as logException (Exception e)
One of the primary disadvantages of error logging is that it adds additional code, and we want to minimize the code we write and maintain. We typically add conditional logic or exception handling blocks in order to detect and log errors.
When something goes wrong inside an application, it becomes easier to debug if we know the source of the error. When an exception is raised, we can log the required information to track down the issue. Python provides a simple and powerful logging library. Let's have a look at how to use logging in Python.
I wouldn't dig too much on external libraries since your logging needs are simple.
.NET Framework already ships with this feature in the namespace System.Diagnostics, you could write all the logging you need there by simply calling methods under the Trace class:
Trace.TraceInformation("Your Information");
Trace.TraceError("Your Error");
Trace.TraceWarning("Your Warning");
And then configure all the trace listeners that fit your needs on your app.config file:
<configuration>
// other config
<system.diagnostics>
<trace autoflush="true" indentsize="4">
<listeners>
<add name="consoleListener" type="System.Diagnostics.ConsoleTraceListener"/>
<add name="textWriterListener" type="System.Diagnostics.TextWriterTraceListener" initializeData="YourLogFile.txt"/>
<add name="eventLogListener" type="System.Diagnostics.EventLogTraceListener" initializeData="YourEventLogSource" />
<remove name="Default"/>
</listeners>
</trace>
</system.diagnostics>
// other config
</configuration>
or if you prefer, you can also configure your listeners in your application, without depending on a config file:
Trace.Listeners.Add(new TextWriterTraceListener("MyTextFile.log"));
Remember to set the Trace.AutoFlush property to true, for the Text log to work properly.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With