【问题标题】:Nlog for logging using NLog API into Application Insight & table storageNlog 用于使用 NLog API 将日志记录到 Application Insight 和表存储中
【发布时间】:2020-03-22 18:03:02
【问题描述】:

我正在尝试使用 NLog 将我的日志记录到 ApplicationInsights & Az 表中。由于我有很多区域,并且我不想将检测密钥保留在配置文件中,因此我正在使用以下方法在 .NET 4.7.1 中编写一个库项目。

 public void AddTelemetryTarget()
        {
            try
            {
                InternalLogger.LogToConsole = true;
                // enable internal logging to a file               
                InternalLogger.LogToConsoleError = true;
                // enable internal logging to a custom TextWriter
                TextWriter writer = File.CreateText(@"D:\My Files\PayloadLog.txt");
                InternalLogger.LogWriter = writer; //e.g. TextWriter writer = File.CreateText("C:\\perl.txt")
                // set internal log level
                InternalLogger.LogLevel = NLog.LogLevel.Trace;

                LogManager.ThrowExceptions = true;
                var config = new LoggingConfiguration();
                //Registering Nlog target with application insight
                ConfigurationItemFactory.Default.Targets.RegisterDefinition("ApplicationInsightsTarget", typeof(ApplicationInsightsTarget));
                ApplicationInsightsTarget aiTarget = new ApplicationInsightsTarget();
                aiTarget.InstrumentationKey = "instrumentationKey";
                aiTarget.Name = "ai";

                var wrapper = new AsyncTargetWrapper(aiTarget, 5000, AsyncTargetWrapperOverflowAction.Grow);
                config.AddTarget("TelemetryAsyncWrapper", wrapper);
                //Applying logging rules.
                LoggingRule rule = new LoggingRule("*", ConvertLogType(LogLevel.Info), aiTarget);
                config.LoggingRules.Add(rule);

                LogFactory logFactory = new LogFactory(config);
                NLog.Logger _logger = logFactory.GetCurrentClassLogger();

                LogEventInfo logEventInfo = new LogEventInfo();
                logEventInfo.Level = NLog.LogLevel.Info;
                logEventInfo.Properties.Add("ModuleName", "TestModule");
                logEventInfo.Properties.Add("MethodName", "TestMethod");
                _logger.Info(logEventInfo);

                logEventInfo = new LogEventInfo();
                logEventInfo.Level = NLog.LogLevel.Error;
                logEventInfo.Properties.Add("ModuleName", "ExceptioModule");
                logEventInfo.Properties.Add("MethodName", "ExceptionMethod");
                _logger.Error(logEventInfo);
            }
            catch (Exception ex)
            {

                throw;
            }
        }

但在 Application Insights 中找不到日志。

我已经添加了上面的 Nuget 包。我错过了什么吗?

内部日志也没有错误

【问题讨论】:

标签: .net nlog


【解决方案1】:

你正在跳入一些陷阱。

永远不要为生产代码激活 LogManager.ThrowExceptions(仅用于故障排除):

LogManager.ThrowExceptions = true;

永远不要使用这个构造函数:

LogFactory logFactory = new LogFactory(config);
NLog.Logger _logger = logFactory.GetCurrentClassLogger();

您应该这样做:

LogManager.Configuration = config;
NLog.Logger _logger = LogManager.GetCurrentClassLogger();

在编写 LogEvent 时,您应该包含一条消息:

var logEventInfo = new LogEventInfo(LogLevel.Info, null, "My custom message");
logEventInfo.Properties["ModuleName"] = "TestModule";
logEventInfo.Properties["MethodName"] = "TestMethod";
_logger.Log(logEventInfo);

当使用异步写入时,你应该记得在 app-exit 之前刷新:

LogManager.Shutdown();

【讨论】:

  • 也许还推荐WithProperty :)
  • 启用我为调试设置的异常。将尝试 cmets 并将恢复。您还可以让我更深入地了解为什么不应该使用它 LogFactory logFactory = new LogFactory(config); NLog.Logger _logger = logFactory.GetCurrentClassLogger();谢谢你:)
  • 因为 LoggingConfiguration 属于为其创建的 LogFactory(使用 LoggingConfiguration 的默认构造函数意味着它属于静态 LogManager.LogFactory)。将 LoggingConfiguration 分配给不同的 LogFactory 可能会产生意想不到的结果。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2023-03-11
  • 1970-01-01
  • 2016-05-02
  • 1970-01-01
  • 1970-01-01
  • 2017-09-29
  • 1970-01-01
相关资源
最近更新 更多