Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Logging with ApplicationInsights

0.00/5 (No votes)
30 Oct 2015 1  
Logging with ApplicationInsights

Introduction

Application Insights includes a powerful Diagnostic Search tool that enables you to explore and drill in to telemetry sent by the Application Insights SDK from your application. Many events such as user page views are automatically sent by the SDK.

You can also write code to send custom events, exception reports, and traces. And if you already use a logging framework such as log4J, log4net, NLog, or System.Diagnostics.Trace, you can capture those logs and include them in the search. This makes it easy to correlate log traces with user actions, exceptions and other events.

This class will help you on logging to Application Insights.

Class Implemented

   public class Logger : ILogger
   {
       private TelemetryClient _appInsightsClient;

       public Logger()
       {
           _appInsightsClient = new TelemetryClient();
           _appInsightsClient.InstrumentationKey = ConfigManager.Insights_Key;
       }

       public void Info(string message)
       {
           var properties = new Dictionary<string, string> { { "message", message } };
           _appInsightsClient.TrackEvent("Info", properties);
       }

       public void Warn(string message)
       {
           var properties = new Dictionary<string, string> { { "message", message } };
           _appInsightsClient.TrackEvent("Warn", properties);
       }

       public void Debug(string message)
       {
           var properties = new Dictionary<string, string> { { "message", message } };
           _appInsightsClient.TrackEvent("Debug", properties);
       }

       public void Error(string message, Exception ex)
       {
           var properties = new Dictionary<string, string> { { "message", message } };
           _appInsightsClient.TrackException(ex, properties);
       }

       public void Error(string message)
       {
           var properties = new Dictionary<string, string> { { "message", message } };
           Exception ex = new Exception(message);
           _appInsightsClient.TrackException(ex, properties);
       }

       public void Error(Exception ex)
       {
           _appInsightsClient.TrackException(ex);
       }
}

Resources

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here