简体   繁体   中英

How to Configure Serilog Logging to Cloudwatch using json

I want to configure Serilog logging to cloud watch by configuring through json. I have a simple WPF application and Serilog logger wrapper class. But when i run the application its not logging to the cloudwatch logs. Would also appreciate if you could guide me on how AWS credentials can be stored and passed to Logger Coniguration.

Below is my Serilog wrapper class,

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,

 { "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.

Install AWS SDK.

Then in VS, go to AWS Explorer, You should be able to configure credential from there.

Create a aws credential file in a specific location with valid credentials and specifiy the same in app.config,

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

My SerilogConfig.json looks like below,

 { "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" } } ] } }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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