[英]Configuring CloudWatch as a target for NLog
I have been trying to set cloudwatch as a target to my Nlog framework in .Net application.我一直在尝试将 cloudwatch 设置为 .Net 应用程序中我的 Nlog 框架的目标。 They haven't mentioned much on their documentation about this.
他们在他们的文档中没有提到太多关于这一点。
This is my Nlog.config file这是我的 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">
<targets>
<target name="aws" type="AWSTarget" logGroup="NLog.ConfigExample" region="us-east-1"/>
<target name="logfile" xsi:type="Console" layout="${callsite} ${message}"/>
</targets>
<rules>
<logger name="*" minlevel="Info" writeTo="logfile,aws" />
</rules
</nlog>
Nothing major just as their sample project says.正如他们的示例项目所说,没什么大不了的。 I have already set AWS credentials in my environment variables.
我已经在我的环境变量中设置了 AWS 凭证。
I have already created a logGroup in cloud watch as well我也已经在 cloud watch 中创建了一个 logGroup
This works for me for a .NET Core application.这适用于我的 .NET Core 应用程序。
using Microsoft.Extensions.Options;
using NLog;
using NLog.AWS.Logger;
using NLog.Config;
using NLog.Layouts;
using System;
namespace AAAA.BBBB.CCCC
{
public interface ILogger
{
void LogError(string message);
void LogError(string message, Exception exception);
void LogInformation(string message);
}
public class Logger : ILogger
{
private static readonly NLog.Logger _logger = LogManager.GetCurrentClassLogger();
public Logger(IOptions<AppSettings> appSettings)
{
var awsSettings = appSettings.Value.AwsSettings;
var awsTarget = new AWSTarget()
{
LogGroup = awsSettings.CloudWatchLogGroup,
Region = awsSettings.Region,
Credentials = new Amazon.Runtime.BasicAWSCredentials(awsSettings.AccessId, awsSettings.AccessKey),
Layout = new SimpleLayout
{
Text = "${longdate} ${level:uppercase=true} ${machinename} ${message} ${exception:format=tostring}"
}
};
var config = new LoggingConfiguration();
config.AddTarget("aws", awsTarget);
config.LoggingRules.Add(new LoggingRule("*", LogLevel.Debug, awsTarget));
LogManager.Configuration = config;
}
public void LogError(string message)
{
_logger.Error(message);
}
public void LogError(string message, Exception exception)
{
_logger.Error(exception, message);
}
public void LogInformation(string message)
{
_logger.Info(message);
}
}
public class AppSettings
{
public AwsSettings AwsSettings { get; set; }
}
public class AwsSettings
{
public string AccessId { get; set; } = default!;
public string AccessKey { get; set; } = default!;
public string CloudWatchLogGroup { get; set; } = default!;
public string Region { get; set; } = default!;
}
}
What if you include <extensions>
? 如果包含
<extensions>
怎么办?
<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
throwConfigExceptions="true">
<extensions>
<add assembly="NLog.AWS.Logger" />
</extensions>
<targets>
<target name="aws" type="AWSTarget" logGroup="NLog" region="us-east-1"/>
<target name="logfile" xsi:type="Console" layout="${callsite} ${message}" />
</targets>
<rules>
<logger name="*" minlevel="Info" writeTo="logfile,aws" />
</rules>
</nlog>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.