Click here to Skip to main content
15,885,366 members
Articles / Hosted Services / Azure
Tip/Trick

Logging with ApplicationInsights

Rate me:
Please Sign up or sign in to vote.
3.57/5 (4 votes)
30 Oct 2015CPOL 22.1K   4   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

C#
   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, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior) Devscope
Portugal Portugal
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

Comments and Discussions

 
SuggestionNo implementation Pin
MSDEVTECHNIK12-Sep-19 5:46
MSDEVTECHNIK12-Sep-19 5:46 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.