private Logger CreateTargetWithGivenInstrumentationKey( string instrumentationKey = "TEST", Action <Logger> loggerAction = null, ApplicationInsightsTarget target = null) { // Mock channel to validate that our appender is trying to send logs TelemetryConfiguration.Active.TelemetryChannel = this.adapterHelper.Channel; if (target == null) { target = new ApplicationInsightsTarget(); } target.InstrumentationKey = instrumentationKey; LoggingRule rule = new LoggingRule("*", LogLevel.Trace, target); LoggingConfiguration config = new LoggingConfiguration(); config.AddTarget("AITarget", target); config.LoggingRules.Add(rule); LogManager.Configuration = config; Logger aiLogger = LogManager.GetLogger("AITarget"); if (loggerAction != null) { loggerAction(aiLogger); target.Dispose(); return(null); } return(aiLogger); }
/// <summary> /// Initializes a new instance of the App class. /// </summary> static App() { WatchStart = Stopwatch.StartNew(); var config = new LoggingConfiguration(); var target = new ApplicationInsightsTarget { InstrumentationKey = Constants.AiKey }; var rule = new LoggingRule("*", LogLevel.Trace, target); config.LoggingRules.Add(rule); LogManager.Configuration = config; Logger.Info( "Popcorn starting..."); DispatcherHelper.Initialize(); LocalizeDictionary.Instance.SetCurrentThreadCulture = true; BlobCache.ApplicationName = "Popcorn"; try { SquirrelAwareApp.HandleEvents( onFirstRun: () => { // TODO: Must complete welcome.md page before activate this feature //_firstRun = true; }); } catch (Exception ex) { Logger.Error(ex); } }
private void SetupLogger() { var config = new LoggingConfiguration(); // You need this only if you did not define InstrumentationKey in ApplicationInsights.config or want to use different instrumentation key var applicationInsightKey = _configuration.GetValue <string>("ApplicationInsights:InstrumentationKey"); var target = new ApplicationInsightsTarget { InstrumentationKey = applicationInsightKey }; // var logfile = new NLog.Targets.FileTarget("logfile") // { // Layout = "${longdate} ${logger} ${level} ${message} ${newline} ${exception} ${exception:format=Type}", // FileName = "${basedir}/logs/Demo.log" // // }; var loggingRule = new LoggingRule("*", LogLevel.Trace, target); config.LoggingRules.Add(loggingRule); // config.AddRule(LogLevel.Debug, LogLevel.Fatal, logfile); // config.AddTarget(logfile); LogManager.Configuration = config; }
private void ConfigureLogging(RuntimeConfiguration runtimeConfiguration, ContainerBuilder builder) { var configuration = new LoggingConfiguration(); Target loggingTarget; if (runtimeConfiguration.GetBool("UseSeq")) { var seqTarget = new SeqTarget { Name = "Seq", ServerUrl = "http://localhost:5341", Properties = { new SeqPropertyItem { Name = "MachineName", Value = "localhost", }, new SeqPropertyItem { Name = "hostname", Value = "${hostname}", }, new SeqPropertyItem { Name = "source", Value = "${callsite:fileName=true}", } }, }; loggingTarget = new BufferingTargetWrapper { Name = "buffer", BufferSize = 1000, FlushTimeout = 2500, WrappedTarget = seqTarget }; } else { loggingTarget = new ApplicationInsightsTarget() { Name = "ApplicationInsights" }; } configuration.AddTarget(loggingTarget); configuration.AddRule(LogLevel.Warn, LogLevel.Fatal, loggingTarget, "Microsoft.*", true); configuration.AddRule(LogLevel.Trace, LogLevel.Fatal, loggingTarget, "*", true); LogManager.Configuration = configuration; builder.RegisterGeneric(typeof(Logger <>)).As(typeof(ILogger <>)); builder.RegisterType <NLogLoggerFactory>().AsImplementedInterfaces().InstancePerLifetimeScope(); }
private static void AddAppInsights(LoggingConfiguration config) { var target = new ApplicationInsightsTarget { Name = "AppInsightsLog" }; config.AddTarget(target); config.AddRule(GetMinLogLevel(), LogLevel.Fatal, "AppInsightsLog"); }
private static TelemetryClient GetTelemetryClient() { var client = new TelemetryClient(); ApplicationInsightsTarget aiTarget = (ApplicationInsightsTarget)NLog.LogManager.Configuration?.FindTargetByName("ai"); if (aiTarget != null) { client.Context.InstrumentationKey = aiTarget.InstrumentationKey; } return(client); }
private static void SetupApplicationInsightsLogging(string appInsightsInstrumentationKey) { var loggingConfiguration = new LoggingConfiguration(); ApplicationInsightsTarget appInsightsTarget = new ApplicationInsightsTarget { InstrumentationKey = appInsightsInstrumentationKey }; loggingConfiguration.LoggingRules.Add(new LoggingRule("*", LogLevel.Warn, appInsightsTarget)); LogManager.Configuration = loggingConfiguration; }
public void GlobalDiagnosticContextPropertiesAreAddedToProperties() { ApplicationInsightsTarget target = new ApplicationInsightsTarget(); target.ContextProperties.Add(new TargetPropertyWithContext("global_prop", "${gdc:item=global_prop}")); Logger aiLogger = this.CreateTargetWithGivenInstrumentationKey(target: target); NLog.GlobalDiagnosticsContext.Set("global_prop", "global_value"); aiLogger.Debug("Message"); var telemetry = (TraceTelemetry)this.adapterHelper.Channel.SentItems.FirstOrDefault(); Assert.AreEqual("global_value", telemetry.Properties["global_prop"]); }
private static LoggingConfiguration CreateLoggingConfiguration() { var config = new LoggingConfiguration(); var target = new ApplicationInsightsTarget { InstrumentationKey = "555b5423-78ac-4329-929c-f5f93439f3c7" }; var rule = new LoggingRule("*", LogLevel.Debug, target); var targetConsole = new ConsoleTarget(); var ruleConsole = new LoggingRule("*", LogLevel.Debug, targetConsole); config.LoggingRules.Add(rule); config.LoggingRules.Add(ruleConsole); return(config); }
public void TraceMessageCanBeFormedUsingLayout() { ApplicationInsightsTarget target = new ApplicationInsightsTarget(); target.Layout = @"${uppercase:${level}} ${message}"; Logger aiLogger = this.CreateTargetWithGivenInstrumentationKey("test", null, target); aiLogger.Debug("Message"); var telemetry = (TraceTelemetry)this.adapterHelper.Channel.SentItems.FirstOrDefault(); Assert.IsNotNull(telemetry, "Didn't get the log event from the channel"); Assert.AreEqual("DEBUG Message", telemetry.Message); }
public void NLogPropertyDuplicateKeyDuplicateValue() { var aiTarget = new ApplicationInsightsTarget(); var logEventInfo = new LogEventInfo(); var loggerNameVal = "thisisaloggername"; logEventInfo.LoggerName = loggerNameVal; logEventInfo.Properties.Add("LoggerName", loggerNameVal); var traceTelemetry = new TraceTelemetry(); aiTarget.BuildPropertyBag(logEventInfo, traceTelemetry); Assert.IsTrue(traceTelemetry.Properties.ContainsKey("LoggerName")); Assert.AreEqual(loggerNameVal, traceTelemetry.Properties["LoggerName"]); }
/// <summary> /// Configure Nlog and set Applications Insights as the target. /// </summary> public static void ConfigureNLogWithAppInsightsTarget() { var appInsightsKey = ConfigurationManager.AppSettings.Get("APPINSIGHTS_INSTRUMENTATIONKEY"); if (!string.IsNullOrEmpty(appInsightsKey)) { var config = new LoggingConfiguration(); ApplicationInsightsTarget target = new ApplicationInsightsTarget(); target.InstrumentationKey = appInsightsKey; LoggingRule rule = new LoggingRule("*", LogLevel.Trace, target); config.LoggingRules.Add(rule); LogManager.Configuration = config; } }
public static void ConfigureNLogWithAppInsightsTarget(IConfigConfigurationProvider configProvider) { var appInsightsKey = configProvider.GetConfig <string>(Constants.ApplicationInsightsInstrumentationKey); if (!string.IsNullOrEmpty(appInsightsKey)) { var config = new LoggingConfiguration(); ApplicationInsightsTarget target = new ApplicationInsightsTarget(); target.InstrumentationKey = appInsightsKey; LoggingRule rule = new LoggingRule("*", LogLevel.Trace, target); config.LoggingRules.Add(rule); LogManager.Configuration = config; } }
/// <summary> /// Initializes a new instance of the App class. /// </summary> static App() { Logger.Info( "Popcorn starting..."); WatchStart = Stopwatch.StartNew(); Directory.CreateDirectory(Constants.Logging); var config = new LoggingConfiguration(); var target = new ApplicationInsightsTarget { InstrumentationKey = Constants.AiKey }; var rule = new LoggingRule("*", LogLevel.Trace, target); config.LoggingRules.Add(rule); LogManager.Configuration = config; DispatcherHelper.Initialize(); LocalizeDictionary.Instance.SetCurrentThreadCulture = true; }
public void GlobalDiagnosticContextPropertiesSupplementEventProperties() { ApplicationInsightsTarget target = new ApplicationInsightsTarget(); target.ContextProperties.Add(new TargetPropertyWithContext("global_prop", "${gdc:item=global_prop}")); Logger aiLogger = this.CreateTargetWithGivenInstrumentationKey(target: target); NLog.GlobalDiagnosticsContext.Set("global_prop", "global_value"); var eventInfo = new LogEventInfo(LogLevel.Trace, "TestLogger", "Hello!"); eventInfo.Properties["Name"] = "Value"; aiLogger.Log(eventInfo); var telemetry = (TraceTelemetry)this.adapterHelper.Channel.SentItems.FirstOrDefault(); Assert.AreEqual("global_value", telemetry.Properties["global_prop"]); Assert.AreEqual("Value", telemetry.Properties["Name"]); }
#pragma warning disable CA1707 // Identifiers should not contain underscores public void EventPropertyKeyNameIsAppendedWith_1_IfSameAsGlobalDiagnosticContextKeyName() #pragma warning restore CA1707 // Identifiers should not contain underscores { ApplicationInsightsTarget target = new ApplicationInsightsTarget(); target.ContextProperties.Add(new TargetPropertyWithContext("Name", "${gdc:item=Name}")); Logger aiLogger = this.CreateTargetWithGivenInstrumentationKey(target: target); NLog.GlobalDiagnosticsContext.Set("Name", "Global Value"); var eventInfo = new LogEventInfo(LogLevel.Trace, "TestLogger", "Hello!"); eventInfo.Properties["Name"] = "Value"; aiLogger.Log(eventInfo); var telemetry = (TraceTelemetry)this.adapterHelper.Channel.SentItems.FirstOrDefault(); Assert.IsTrue(telemetry.Context.Properties.ContainsKey("Name_1"), "Key name altered"); Assert.AreEqual("Global Value", telemetry.Context.Properties["Name"]); Assert.AreEqual("Value", telemetry.Context.Properties["Name_1"]); }
protected void Application_Start() { GlobalConfiguration.Configure(WebApiConfig.Register); ApplicationInsightsTarget aiTarget = new ApplicationInsightsTarget(); aiTarget.InstrumentationKey = ConfigurationManager.AppSettings["APPINSIGHTS_INSTRUMENTATIONKEY"]; aiTarget.Name = "ai"; LogManager.Configuration.AddTarget(aiTarget); LoggingRule rule = new LoggingRule("*", LogLevel.Trace, aiTarget); LogManager.Configuration.LoggingRules.Add(rule); var logger = LogManager.GetLogger("Global Logger"); TelemetryClient telemetry = new TelemetryClient(); telemetry.TrackEvent("WinGame"); logger.Error("Application Has Started"); }