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