Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Simple way to perform error logging?

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.

like image 723
Analytic Lunatic Avatar asked Oct 07 '22 02:10

Analytic Lunatic


People also ask

What is error logging in C#?

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.

What are the various types of error logs?

Errors are divided into the following three categories: predicted, recoverable errors. predicted, unrecoverable errors. unpredicted errors.

What is the purpose of error logging?

The error-logging facility records hardware and software failures in the error log for information purposes or for fault detection and corrective action.

How do I log an error in Python?

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 is wrong with using logging methods in Java?

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)

What are the disadvantages of error logging?

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.

What is Python logging and how to use it?

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.


1 Answers

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.

like image 186
Mauro2 Avatar answered Oct 09 '22 03:10

Mauro2