Build status NuGet

Logging from System.Diagnostics

.NET comes with its own tracing/logging feature located in the System.Diagnostics namespaces. A core part of System.Diagnostics is the Trace class, but that namespace contains utilities for performance counters, working with the event log and a lot of other features. In this article, we will focus on logging to from System.Diagnostics.Trace.

To start logging, install the Elmah.Io.Trace package:

Install-Package Elmah.Io.Trace

As default, Trace logs to the Win32 OutputDebugString function, but it is possible to log to multiple targets (like appenders in log4net). To do so, tell Trace about

    new ElmahIoTraceListener("API_KEY", new Guid("LOG_ID")));

Replace API_KEY with your API key (Where is my API key?) and LOG_ID with your log id (Where is my log ID?).

To start logging, call the Trace API:

    System.DIagnostics.Trace.Write("Starting something dangerous");
catch (Exception e)
    System.Diagnostics.Trace.Fail(e.Message, e.ToString());

In the example, we write an information message with the message Starting something dangerous and logs any thrown exception to

This article was brought to you by the team. is the best error management system for .NET web applications. We monitor your website, alert you when errors start happening and help you fix errors fast.

Monitor your website