简体   繁体   English

如何使用 json 配置 Serilog 日志记录到 Cloudwatch

[英]How to Configure Serilog Logging to Cloudwatch using json

I want to configure Serilog logging to cloud watch by configuring through json.我想通过 json 配置将 Serilog 日志记录配置到云监视。 I have a simple WPF application and Serilog logger wrapper class.我有一个简单的 WPF 应用程序和 Serilog 记录器包装类。 But when i run the application its not logging to the cloudwatch logs.但是当我运行应用程序时,它没有记录到 cloudwatch 日志。 Would also appreciate if you could guide me on how AWS credentials can be stored and passed to Logger Coniguration.如果您能指导我如何存储 AWS 凭证并将其传递给 Logger Coniguration,也将不胜感激。

Below is my Serilog wrapper class,下面是我的 Serilog 包装类,

public class SerilogLogger : ILog
{
    private readonly ILogger _logger;

    public SerilogLogger(IConfiguration configuration)
    {
        _logger = new LoggerConfiguration()
            .ReadFrom.Configuration(configuration)
            .CreateLogger();
        Log.Logger = _logger;
    }

    public void Debug(string format, params object[] args)
    {
        _logger.Debug(format, args);
    }

    public void Error(Exception ex)
    {
        _logger.Error(ex,"Error");
    }

    public void Error(Exception ex, string format, params object[] args)
    {
        _logger.Error(ex, format, args);
    }

    public void Fatal(Exception ex, string format, params object[] args)
    {
        _logger.Fatal(ex, format, args);
    }

    public void Info(string format, params object[] args)
    {
        _logger.Information(format,args);
    }

    public void Trace(string format, params object[] args)
    {
        _logger.Verbose(format, args);
    }

    public void Warn(string format, params object[] args)
    {
        _logger.Warning(format,args);
    }

    public void Close()
    {
        Log.CloseAndFlush();
    }
}

And the wrapper is called from my startup page,从我的启动页面调用包装器,

private readonly ILog _logger;
    private int _count = 0;
    public MainWindow()
    {
        InitializeComponent();
        var configuration = new ConfigurationBuilder()
            .AddJsonFile("SerilogConfig.json")
            .Build();
        _logger = new SerilogLogger(configuration);
        _logger.Info("Click count is {0}", _count += 1);
    }

And i have the SerilogConfig.json file,我有 SerilogConfig.json 文件,

 { "Serilog": { "Using": [ "Serilog.Sinks.File", "AWS.Logger.SeriLog" ], "MinimumLevel": "Verbose", "Region": "us-east-2", "LogGroup": "Petnet-Olympus-Serilog", "Enrich": [ "FromLogContext", "WithMachineName", "WithProcessId", "WithThreadId", "WithExceptionDetails", "WithAssemblyName", "WithAssemblyVersion" ], "WriteTo": [ { "Name": "File", "Args": { "path": "logs/Olympus_Serilog_.txt", "rollingInterval": "Day", "outputTemplate": "{Timestamp:o} [{Level}] ({MachineName}/{AssemblyName}-{AssemblyVersion}/{ThreadId}) {Message}{NewLine}{Exception}" } }, { "Name": "File", "Args": { "path": "logs/Olympus_Serilog_.json", "rollingInterval": "Day", "formatter": "Serilog.Formatting.Json.JsonFormatter, Serilog" } }, { "Name": "AWSSeriLog" } ] } }

I am unable to get the logging to happen in Cloudwatch, Also would like to know how AWS Credentials has to be kept and how the Logger would read the credentials.我无法在 Cloudwatch 中进行日志记录,还想知道必须如何保留 AWS 凭证以及记录器如何读取凭证。

Install AWS SDK.安装 AWS 开发工具包。

Then in VS, go to AWS Explorer, You should be able to configure credential from there.然后在 VS 中,转到 AWS Explorer,您应该能够从那里配置凭证。

Create a aws credential file in a specific location with valid credentials and specifiy the same in app.config,使用有效凭据在特定位置创建一个 aws 凭据文件,并在 app.config 中指定相同内容,

<appSettings>
<!-- AWS -->
<add key="AWSProfilesLocation" value="D:\awscredentials" />
</appSettings>

My SerilogConfig.json looks like below,我的 SerilogConfig.json 如下所示,

 { "Serilog": { "Using": [ "Serilog.Sinks.File", "AWS.Logger.SeriLog" ], "MinimumLevel": "Verbose", "Enrich": [ "WithMachineName", "FromLogContext", "WithExceptionDetails", "WithAssemblyInformationalVersion" ], "Properties": { "Application": "App" }, "Region": "us-east-1", "Profile": "serilog", "LogGroup": "CloudGroup-Client", "LibraryLogFileName": "serilog-aws-errors.txt", "LogLevel": { "Default": "Information", "System": "Information", "Microsoft": "Information" }, "WriteTo": [ { "Name": "AWSSeriLog", "Args": { "textFormatter": "Serilog.Formatting.Json.JsonFormatter, Serilog" } } ] } }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM