ApplicationInsights-dotnet/LOGGING
Rajkumar Rangaraj e7385f3234 Change event message 2020-08-03 09:14:20 -07:00
..
src Change event message 2020-08-03 09:14:20 -07:00
test remove net45 net451 (#1962) 2020-07-09 17:13:35 -07:00
.gitattributes Migrate LoggingSDK to Base repo (#1263) 2019-10-25 11:41:35 -07:00
.gitignore Migrate LoggingSDK to Base repo (#1263) 2019-10-25 11:41:35 -07:00
35MSSharedLib1024.snk Migrate LoggingSDK to Base repo (#1263) 2019-10-25 11:41:35 -07:00
CodeCov.ps1 Migrate LoggingSDK to Base repo (#1263) 2019-10-25 11:41:35 -07:00
Directory.Build.props refactor props (#1907) 2020-06-15 14:01:23 -07:00
LICENSE Migrate LoggingSDK to Base repo (#1263) 2019-10-25 11:41:35 -07:00
Logging.sln Removing product/test.props to normalize output paths (#1919) 2020-06-15 17:30:40 -07:00
NuGet.Config Tilee/everything (#1275) 2019-11-07 13:22:11 -08:00
NuGet.exe Migrate LoggingSDK to Base repo (#1263) 2019-10-25 11:41:35 -07:00
PushNugetPackages.cmd Migrate LoggingSDK to Base repo (#1263) 2019-10-25 11:41:35 -07:00
README.md Update NLog examples (#1691) 2020-02-18 16:59:09 -08:00
SourceRoot.marker Migrate LoggingSDK to Base repo (#1263) 2019-10-25 11:41:35 -07:00
ThirdPartyNotices.txt Migrate LoggingSDK to Base repo (#1263) 2019-10-25 11:41:35 -07:00
VSOPushNugetPackages.cmd Migrate LoggingSDK to Base repo (#1263) 2019-10-25 11:41:35 -07:00
buildDebug.cmd Migrate LoggingSDK to Base repo (#1263) 2019-10-25 11:41:35 -07:00
buildRelease.cmd Migrate LoggingSDK to Base repo (#1263) 2019-10-25 11:41:35 -07:00
clean.cmd Migrate LoggingSDK to Base repo (#1263) 2019-10-25 11:41:35 -07:00

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 your application does not have web.config then it can also be configured manually.

  • Configure ApplicationInsightsTarget using NLog.config :
<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <extensions>
		<add assembly="Microsoft.ApplicationInsights.NLogTarget" />
    </extensions>
	<targets>
		<target xsi:type="ApplicationInsightsTarget" name="aiTarget">
			<instrumentationKey>Your_Resource_Key</instrumentationKey>	<!-- Only required if not using ApplicationInsights.config -->
			<contextproperty name="threadid" layout="${threadid}" />	<!-- Can be repeated with more context -->
		</target>
	</targets>
	<rules>
		<logger name="*" minlevel="Trace" writeTo="aiTarget" />
	</rules>
</nlog>

NLog allows you to configure conditional configs:

<instrumentationKey>${configsetting:APPINSIGHTS.INSTRUMENTATIONKEY:whenEmpty=${environment:APPINSIGHTS_INSTRUMENTATIONKEY}}</instrumentationKey>	

For more information see:

// You need this only if you did not define InstrumentationKey in ApplicationInsights.config (Or in the NLog.config)
TelemetryConfiguration.Active.InstrumentationKey = "Your_Resource_Key";

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

logger.Trace("trace log message");
  • Configure ApplicationInsightsTarget using NLog Config API : If you configure NLog programmatically with the NLog Config API, then 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>