.NET Logging adaptors
Branch: develop
Clone or download
lmolkova Merge pull request #264 from Microsoft/lmolkova-patch-1
Microsoft.Extensions.Logging.applicaitonInsights README: Fix indentation and nits
Latest commit 471b47b Feb 15, 2019
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
HelloWorldTest Merge branch 'develop' into tilee/update_dependencies Jun 28, 2018
SDL sdl script revision (#142) Jan 12, 2018
src Fix indentation and nits Feb 15, 2019
test Update to Base SDK 2.9.1 (#259) Feb 7, 2019
.gitattributes fix stylecop (#117) Sep 2, 2017
.gitignore Initial commit Nov 13, 2015
35MSSharedLib1024.snk Initial commit Nov 13, 2015
AddXmlLanguage.targets Add Xml Doc back to Nuget Packages Jul 25, 2018
ApplicationInsightsSDKRules.ruleset disable fxcop "Identifiers should not match keywords" (#217) Sep 13, 2018
CHANGELOG.md Update to Base SDK 2.9.1 (#259) Feb 7, 2019
CodeCov.ps1 revert bad edit of codecov.ps1 (#136) Feb 7, 2018
Common.props Fix fxcop running on netstandard2.0 and failing Dec 31, 2018
Common.targets Added injection of language into XML documentation files. Sep 8, 2016
Directory.Build.props 4 of many... convert two more projects to generate it's own package (#… Nov 21, 2017
EnlistmentRoot.marker Initial commit Nov 13, 2015
GenerateReleaseMetadata.ps1 new script: GenerateReleaseMetadata (#170) Apr 11, 2018
GlobalStaticVersion.props Update to Base SDK 2.9.1 (#259) Feb 7, 2019
LICENSE License and Documentation Nov 13, 2015
Logging.sln Adding unit tests and shifting from using TelemetryConfiguration to I… Dec 18, 2018
Microsoft-Security-Recommended.ruleset Added rules and enabled code analysis on build. Aug 12, 2016
NuGet.Config Add myget feed to nuget.config Jan 2, 2019
NuGet.exe add script to audit nupkg during build (#223) Oct 8, 2018
NugetAudit.ps1 LicenseUrl to LicenseExpression Jan 29, 2019
PushNugetPackages.cmd Initial commit Nov 13, 2015
README.md cleanup Jan 30, 2019
Signing.targets Initial commit Nov 13, 2015
Test.props Switched from stelycop msbuild to stelycop analyzer (#125) Nov 10, 2017
ThirdPartyNotices.txt Adding third party notices Sep 7, 2016
VSOPushNugetPackages.cmd Update nuget package path Jan 22, 2016
buildDebug.cmd Migrate EventSourceListener project to netstandard1.3 Mar 30, 2017
buildRelease.cmd Migrate EventSourceListener project to netstandard1.3 Mar 30, 2017
clean.cmd Tilee/fix unittests2 (#146) Feb 9, 2018
dirs.proj Migrate EventSourceListener project to netstandard1.3 Mar 30, 2017
disablestrongnamevalidation.ps1 Added ps scripts to enable disable strong name verification. This is … Aug 10, 2017
enablestrongnamevalidation.ps1 Added ps scripts to enable disable strong name verification. This is … Aug 10, 2017

README.md

Build codecov.io

Nuget packages

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.

Read more:

ILogger

See this.

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).

For more information, see NLog Documentation

  • 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:
var config = new LoggingConfiguration();

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");

Log4Net

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

For more information, see Log4Net Configuration

// 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.

For more information, see "Microsoft Docs: "Tracing and Instrumenting Applications"

<configuration>
  <system.diagnostics>
    <trace>
      <listeners>
        <add name="myAppInsightsListener" type="Microsoft.ApplicationInsights.TraceListener.ApplicationInsightsTraceListener, Microsoft.ApplicationInsights.TraceListener" />
      </listeners>
    </trace>
  </system.diagnostics>
</configuration>

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");

EventSource

EventSourceTelemetryModule allows you to configure EventSource events to be sent to Application Insights as traces.

For more information, see Microsoft Docs: "Using EventSource Events".

ETW

EtwCollectorTelemetryModule allows you to configure events from ETW providers to be sent to Application Insights as traces.

For more information, see Microsoft Docs: "Using ETW Events".

DiagnosticSource

You can configure System.Diagnostics.DiagnosticSource events to be sent to Application Insights as traces.

For more information, see CoreFX: "Diagnostic Source Users Guide".

To enable, edit the TelemetryModules section of the ApplicationInsights.config file:

<Add Type="Microsoft.ApplicationInsights.DiagnsoticSourceListener.DiagnosticSourceTelemetryModule, Microsoft.ApplicationInsights.DiagnosticSourceListener">
      <Sources>
        <Add Name="MyDiagnosticSourceName" />
      </Sources>
 </Add>