首页 > 解决方案 > AWS NLog:更新 AWSTarget.LogStreamPrefix 以在运行时修改 Cloudwatch 流名称

问题描述

AWS 目标只是决定在更改代码以更新其流名称后不记录任何内容。不理会它会正常记录,但是如果我执行我在下面的代码中输入的操作,它会踢桶并且不会将任何内容记录到云手表,并且在将 NLog.config 修改为 internalLogToConsole="true" 时也不会出现错误

这是一个易于使用的示例 repo,如果您在本地有 aws 凭据,它应该可以在拍摄时工作,如果您不对 LogSramName GitHub Repo Sample进行随机修改,它会起作用

NLog.config

<?xml version="1.0" encoding="utf-8" ?>
<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      throwExceptions="true" internalLogToConsole="true">
  <targets>
    <target name="aws" type="AWSTarget" logGroup="ForumHarvester" region="us-east-1"/>
    <target name="logfile" xsi:type="Console" layout="${callsite} ${message}" />
  </targets>
  <rules>
    <logger name="*" minlevel="Info" writeTo="logfile,aws" />
  </rules>
</nlog>

Nuget 依赖项:

完全破坏 awstarget 而不是重命名其流的代码

if (filekey != string.Empty)
            {
                var config = NLog.LogManager.Configuration;
                var awstarget = config.FindTargetByName("aws") as NLog.AWS.Logger.AWSTarget;
                awstarget.LogStreamNamePrefix = filekey.Replace("--", "-id:") + "---";
                NLog.LogManager.Configuration = config; //Its implied that this will reload the configs
                // and all will be right with the world, thats not my experience so far

                 //NLog.LogManager.ReconfigExistingLoggers(); 
                 // I was also trying this because it looks promising but no dice
            }

调试日志

这是我通过内部登录获得的示例:

2020-07-22 07:30:27.4953 Info Auto loading assembly file: C:\...\bin\Debug\netcoreapp3.1\NLog.AWS.Logger.dll
2020-07-22 07:30:27.5796 Info Loading assembly file: C:\...\bin\Debug\netcoreapp3.1\NLog.AWS.Logger.dll
2020-07-22 07:30:27.6157 Info NLog.AWS.Logger, Version=1.5.2.0, Culture=neutral, PublicKeyToken=885c28607f98e604. File version: 1.5.2.0. Product version: 1.5.2. GlobalAssemblyCache: False
2020-07-22 07:30:27.6717 Info Auto loading assembly file: C:\...\bin\Debug\netcoreapp3.1\NLog.AWS.Logger.dll succeeded!
2020-07-22 07:30:27.6717 Info Message Template Auto Format enabled
2020-07-22 07:30:27.7137 Info Adding target AWSTarget(Name=aws)
2020-07-22 07:30:27.7226 Info Adding target ConsoleTarget(Name=logfile)
2020-07-22 07:30:27.7721 Info LogManager.ThrowExceptions = true can crash the application! Use only for unit-testing and last resort troubleshooting.
2020-07-22 07:30:27.7721 Info Validating config: TargetNames=logfile, aws, ConfigItems=17, FilePath=C:\...\bin\Debug\netcoreapp3.1\NLog.config
2020-07-22 07:30:28.1910 Info Configuration initialized.
2020-07-22 07:30:28.1910 Info NLog, Version=4.0.0.0, Culture=neutral, PublicKeyToken=5120e14c03d0593c. File version: 4.7.2.11786. Product version: 4.7.2+024896f64b840a743d3fd89e29c2186681e7795f. GlobalAssemblyCache: False
2020-07-22 07:51:49.1766 Info Closing old configuration.
2020-07-22 07:51:49.2775 Info Validating config: TargetNames=logfile, aws, ConfigItems=17, FilePath=C:\...\bin\Debug\netcoreapp3.1\NLog.config
2020-07-22 07:51:49.2775 Info Validating config: TargetNames=logfile, aws, ConfigItems=17, FilePath=C:\...\bin\Debug\netcoreapp3.1\NLog.config
...Application.Program.Main --IN MAIN--
...Application.Program.Main MAIN ARGUMENTS: -c interactive -a Application --tt false --filebucket jobs

--IN MAIN-- 从不出现在 cloudwatch 中,但显然在屏幕/控制台日志中。

参考资料: https ://github.com/NLog/NLog/wiki/Configure-from-code , https://github.com/aws/aws-logging-dotnet/issues/83 , https://github.com /aws/aws-logging-dotnet/issues/1

标签: c#amazon-web-services.net-corenlog

解决方案


推荐阅读