Logging to elmah.io from System.Diagnostics

Logging through System.Diagnostics have been deprecated. Please use the Elmah.Io.Client package to log trace messages to elmah.io.

.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 elmah.io from System.Diagnostics.Trace.

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

Install-Package Elmah.Io.Trace
dotnet add package Elmah.Io.Trace
<PackageReference Include="Elmah.Io.Trace" Version="3.*" />
paket add 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 elmah.io:

System.Diagnostics.Trace.Listeners.Add(
    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:

try
{
    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 log any thrown exception to elmah.io.


This article was brought to you by the elmah.io team. elmah.io 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.

See how we can help you monitor your website for crashes Monitor your website