Skip to content

calebjenkins/ApplicationInsights-dotnet-logging

 
 

Repository files navigation

Build

Application Insights logging adapters.

If you use NLog, log4Net or System.Diagnostics.Trace for diagnostic tracing in your application, you can have your logs sent to Application Insights, where you can explore and search them. Your logs will be merged with the other telemetry coming from your application, so that you can identify the traces associated with servicing each user request, and correlate them with other events and exception reports.

Application Insights Documentation.

#NLog Application Insights NLog Target nuget package adds ApplicationInsights target in your web.config (If you use application type that does not have web.config you can install the package but you need to configure ApplicationInsights programmatically; see below).

  • If you configure NLog though web config then you just need do the following:
// You need this only if you did not define InstrumentationKey in ApplicationInsights.config
TelemetryConfiguration.Active.InstrumentationKey = "Your_Resource_Key";

Logger logger = LogManager.GetLogger("Example");

logger.Trace("trace log message");
  • If you configure NLog programmatically than create Application Insights target in code and add it to your other targets:
ApplicationInsightsTarget target = new ApplicationInsightsTarget();
// You need this only if you did not define InstrumentationKey in ApplicationInsights.config or want to use different instrumentation key
target.InstrumentationKey = "Your_Resource_Key";

LoggingRule rule = new LoggingRule("*", LogLevel.Trace, target);
config.LoggingRules.Add(rule);

LogManager.Configuration = config;

Logger logger = LogManager.GetLogger("Example");

logger.Trace("trace log message");

NLog Documentation

#Log4Net

Application Insights Log4Net adapter nuget modifies web.config and adds Application Insights Appender.

// You do not need this if you have instrumentation key in the ApplicationInsights.config
TelemetryConfiguration.Active.InstrumentationKey = "Your_Resource_Key";

log4net.Config.XmlConfigurator.Configure();
var logger = LogManager.GetLogger(this.GetType());

logger.Info("Message");
logger.Warn("A warning message");
logger.Error("An error message");

#System.Diagnostics

Microsoft.ApplicationInsights.TraceListener nuget package modifies web.config and adds application insights listener. (If your application type does not have web.config, add listener programmatically or in the configuration file appropriate to your application type)

// You do not need this if you have instrumentation key in the ApplicationInsights.config
TelemetryConfiguration.Active.InstrumentationKey = "Your_Resource_Key";
System.Diagnostics.Trace.TraceWarning("Slow response - database01");

##Nuget packages

About

.NET Logging adaptors

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • C# 88.3%
  • PowerShell 9.4%
  • Batchfile 2.1%
  • Pascal 0.2%