Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Simple Debug and Logging using System.Diagnostics

I wish to be able to write entries to a console application which will describe when actions have been completed, possibly writing them to a .txt file at one point.

I would like it to be used with a separate GUI application running at the same time so i can use the application and monitor the log simultaneously.

I only assume the Diagnostic class is the right tool to use however I have never used any logging methods before, so i welcome any other suggestions.

Thanks

like image 536
Jamie Keeling Avatar asked Jan 07 '10 02:01

Jamie Keeling


People also ask

What is diagnostic logging?

Diagnostic logging is a troubleshooting mode. When diagnostic logging is enabled, Google Ads Editor automatically saves reports containing messages sent between Google Ads Editor and the Google Ads server.

How to write Logging in C#?

Each file is written by a separate logger, so you need to use both inside of your code: ILog Log = LogManager. GetLogger(typeof(LogTest)); ILog ErrorLog = LogManager. GetLogger("error"); Log.Info("Debug message"); ErrorLog.

What is Logging in C#?

Logging levels are used to filter logging output, tailoring the amount of data output to the situation in hand. Each logging level is associated with the type of data logged. DEBUG, INFO, and TRACE events are typically non-error conditions that report on the behavior of an application.


2 Answers

Look at System.Diagnostics.Trace. You can add different TraceListeners to it, including listeners for the Console or files. Then replace all your Console.Write()/Console.WriteLine() calls with Trace.Write()/Trace.WriteLine() and you're good. You can even implement your own TraceListener (it's very easy) to send the messages to your GUI app.

like image 56
Joel Coehoorn Avatar answered Sep 29 '22 00:09

Joel Coehoorn


The $0.25 solution is Project + Properties, Application tab, Output type = Console Application. Now you've got a console window as well as your regular UI. Anything you write with Console.WriteLine() will end up on that console window.

like image 27
Hans Passant Avatar answered Sep 29 '22 00:09

Hans Passant