Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
487 views
in Technique[技术] by (71.8m points)

c# - AWS.Logger.AspNetCore with .NET Core 3.1 Web API project - log message formatting and delivery questions

I am currently using AWS.Logger.AspNetCore v3.0.1 as my logging system inside a .Net Core Web API project.

I've got my logging set up simply in Program.cs, like so:

                .ConfigureLogging(logging =>
                {
                    logging.AddAWSProvider();
                }

with the log stream and flags set in appsettings.json. So far, so good.

I was wondering if anyone could possibly help with the following:

  1. I'd like to prepend text to all the messages logged - even before the log level, category, scope.

I know there is an overload of AddAWSProvider() which lets me supply my own custom message formatter which AWSLogger instance uses. I could add this to Program.cs for example:

                [IHostBuilder].ConfigureLogging(logging =>
                {
                    logging.AddAWSProvider(((level, state, exception) =>
                    {
                        // TODO: Add formatting code which returns a formatted log entry string here
                    }));
                })

however this approach doesn't give me access to the "Scope" supplied to ILogger.BeginScope() which I need to see my connection ID, trace ID etc.

Any ideas on how I can intercept the full string (not just the message!) to be sent to AWS and then transform it? I don't have a lot of time left to spend on this, so switching to something like Log4Net, Serilog or creating a full LogProvider or ScopeProvider is not an option.

Even something that auto-prefixes a custom formatted TimeStamp would be satisfactory. (And yes, I know AWS CloudWatch logs display a timestamp, but not when you "View As Text" )

  1. Are there any docs on the AWSLoggerCore daemon? (AWS.Logger.Core.AWSLoggerCore::StartMonitor() and Monitor() )

I've looked at the code and noticed it dispatches to AWS a max of 5 requests (log messages) per second. However, I don't know how regularly it checks for new messages, and whether we can control that to ensure messages are logged immediately. Nor do I know how often or under what circumstances it calls Flush(). Losing vital log messages could prevent us tracing future issues.

The API runs within an EC2 container.

Thanks in advance, Scott


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Maybe you can try to create a decorator for the ILogger instance:

namespace ConsoleSample
{
    public class Program
    {
        public static void Main(string[] args)
        {
            var config = new AWS.Logger.AWSLoggerConfig("AspNetCore.ConsoleSample");
            config.Region = "us-east-1";

            LoggerFactory logFactory = new LoggerFactory();

            logFactory.AddAWSProvider(config);

            // use the Decorator instead
            var logger = new LoggerDecorator<Program>(logFactory.CreateLogger<Program>());

            logger.LogInformation("Check the AWS Console CloudWatch Logs console in us-east-1");
            logger.LogInformation("to see messages in the log streams for the");
            logger.LogInformation("log group AspNetCore.ConsoleSample");
        }
    }
}

namespace ConsoleSample
{
    public class LoggerDecorator<T> : ILogger<T>
    {
        private readonly ILogger<T> _logger;
        public LoggerDecorator(ILogger<T> logger)
        {
            _logger = logger;
        }

        public void Log<TState>(LogLevel logLevel, EventId eventId, TState state, Exception exception, Func<TState, Exception, string> formatter)
        {
            string message = $"{DateTime.UtcNow} [{logLevel.ToString().ToUpper()}] {state}";
            _logger.Log(logLevel, eventId, message, exception, formatter);
        }

        public bool IsEnabled(LogLevel logLevel)
        {
            return _logger.IsEnabled(logLevel);
        }

        public IDisposable BeginScope<TState>(TState state)
        {
            return _logger.BeginScope(state);
        }
    }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...