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
I am João Sousa, and since i finish my degree I’m working in software development using Microsoft technologies.
I was awarded
Microsoft Most Valuable Professional (MVP) 2015 – .Net
My profissional profile:
Azure Developer
.NET Developer
My Certifications:
MCTS - .NET Framework - Application Development Foundation
MCTS - .NET Framework 2.0 - Windows-based Client Development
MCTS - .NET Framework 3.5 ADO.NET Applications
MCTS - .NET Framework 3.5 ASP.NET Applications
MCSD - Programming in HTML5 with JavaScript and CSS3
MCSD - Developing ASP.NET MVC 4 Web Applications
MCSD - Developing Windows Azure and Web Services
MCSA Office 365 - Managing Office 365 Identities and Requirements
MCSA Office 365 - Enabling Office 365 Services
MCSD - Implementing Microsoft Azure Infrastructure Solutions