Getting started with Application Insights (AI) in a new or existing application is very easy. From Visual Studio 2013 Update 3 it is even integrated right into the New Web Project dialog:
NB: you can also perform this operation later on, by select XXXX from the context menu in Solution Explorer.
Now, this is very nice for a new project but what if you have an existing application that already contains a lot of code for writing trace and debug information? Perhaps you are using Log4Net or the TraceListener class to emit diagnostic information in some way. Well, the good news is that the nice fellows over at Microsoft thought about this. They have created a set of public NuGet packages that makes it very easy to forward the information that you are logging to Application Insights. This means that you won’t have to rewrite any of the existing code and still have the tracing information show up in the portal.
For example, if you are using Log4Net you can use the Microsoft.ApplicationInsights.Log4NetAppender package that will send the information you log using the Log4Net API to AI. It is implemented as a standard Log4Net Appender class, which makes it very easy to use. Just add the package to the projects that performs logging, and the following information will be added to your configuration file:
<log4net> <root> <level value="ALL"/> <appender-ref ref="aiAppender"/> </root> <appender name="aiAppender" type="Microsoft.ApplicationInsights.Log4NetAppender.ApplicationInsightsAppender, Microsoft.ApplicationInsights.Log4NetAppender"> <layout type="log4net.Layout.PatternLayout"> <conversionPattern value="%message%newline"/> </layout> </appender> </log4net>
Now, you existing logging Log4Net code..
log4net.Config.XmlConfigurator.Configure(); var logger = log4net.LogManager.GetLogger(this.GetType()); logger.Info("Some information message"); logger.Warn("A warning message"); logger.Error("An error message");
Will end up as Trace information in the Azure Portal;
Since this is a regular log4net appender, you can apply the standard filters, to include or exclude certain types of information.
Microsoft has also implemented NuGet packages for NLog and also a TraceListener class, for use when you do standard .NET tracing. In addition, there are some 3rd party packages that covers other logging frameworks such as Serilog: