Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error logging in C#

I am making my switch from coding in C++ to C#. I need to replace my C++ error logging/reporting macro system with something similar in C#.

In my C++ source I can write

LOGERR("Some error"); or LOGERR("Error with inputs %s and %d", stringvar, intvar);

The macro & supporting library code then passes the (possibly varargs) formatted message into a database along with the source file, source line, user name, and time. The same data is also stuffed into a data structure for later reporting to the user.

Does anybody have C# code snippets or pointers to examples that do this basic error reporting/logging?

Edit: At the time I asked this question I was really new to .NET and was unaware of System.Diagnostics.Trace. System.Diagnostics.Trace was what I needed at that time. Since then I have used log4net on projects where the logging requirements were larger and more complex. Just edit that 500 line XML configuration file and log4net will do everything you will ever need :)

like image 255
Rodney Schuler Avatar asked Sep 29 '08 04:09

Rodney Schuler


People also ask

What is error logging?

error log. The file that stores instances of errors and failures encountered by the system. error log entry. A record in the system error log that describes a hardware failure, a software failure, or an operator message. An error log entry contains captured failure data.

What is error checking in C?

Global Variable errno: When a function is called in C, a variable named as errno is automatically assigned a code (value) which can be used to identify the type of error that has been encountered. Its a global variable indicating the error occurred during any function call and defined in the header file errno. h.

How do I check error logs?

Press CTRL + F for Windows or Command + F for MacOS to open the search bar on your web browser. Type log_errors to find the log_errors row. If the value is Off, then the PHP error logging is disabled.

What are the various types of error logs?

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


1 Answers

Lots of log4net advocates here so I'm sure this will be ignored, but I'll add my own preference:

System.Diagnostics.Trace 

This includes listeners that listen for your Trace() methods, and then write to a log file/output window/event log, ones in the framework that are included are DefaultTraceListener, TextWriterTraceListener and the EventLogTraceListener. It allows you to specify levels (Warning,Error,Info) and categories.

Trace class on MSDN
Writing to the Event Log in a Web Application
UdpTraceListener - write log4net compatible XML messages to a log viewer such as log2console

like image 120
Chris S Avatar answered Sep 18 '22 14:09

Chris S