Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

AWS logger logs to cloudwatch twice #199

Open
lanierhall opened this issue Apr 14, 2022 · 4 comments
Open

AWS logger logs to cloudwatch twice #199

lanierhall opened this issue Apr 14, 2022 · 4 comments
Labels
bug This issue is a bug. module/logging p2 This is a standard priority issue queued

Comments

@lanierhall
Copy link

Describe the bug

AWS logger code initializes twice in certain cases.

Expected Behavior

It should only log once

Current Behavior

When calling LogManager.GetLogger it calls initialization code for the logger/appender which calls
AppConfigAWSCredentials which calls GetLogger on line 33 which starts logger code initialization all over again. The second attempt finds credentials as they were set in the first one and creates a listener. The first stack finally returns in AppConfigAWSCredentials and logs that credentials were found and then continues to setup/add a listener. There are now two listeners that both react to logs coming in and send them to cloudwatch. The fix I used was to add lock/isInitialized functionality.

Reproduction Steps

Create a project and add

    private static readonly ILog Logger = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);

And use the logger somewhere in main.
Add the following sections to your config:

... ...
<appender name="Cloudwatch" type="AWS.Logger.Log4net.AWSAppender,AWS.Logger.Log4net">
  <LogGroup>SystemDeveloper</LogGroup>
  <Region>us-east-1</Region>
  <layout type="log4net.Layout.PatternLayout">
    <conversionPattern value="%property{log4net:HostName} [%thread] %-5level %logger - %message%newline"/>
  </layout>
  <LibraryLogFileName>c:\logs\awslog.txt</LibraryLogFileName>
</appender>

Additionally:

  • There should be no AWS Credential providers other than the AppConfigAWSCredentials
  • In your app.config you should add an AWS key and secret entry
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<appSettings>
    <add key="AWSAccessKey" value="valid key"/>
    <add key="AWSSecretKey" value="valid secret"/>
...

To reproduce the issue, you need to cause the Amazon.Runtime.AppConfigAWSCredentials object to instantiate. This is done when the FallbackCredentialsFactory intantiates the CredentialsGenerators property. In other words, create a project with aws key/secret in your app.config project and have no other way to provide aws credentials so that it looks for them there.

Possible Solution

No response

Additional Information/Context

No response

AWS .NET SDK and/or Package version used

AWS.Logger.Core 3.1.0
AWS.Logger.Log4net 3.3.0
log4net 2.0.12
AWSSDK.CloudWatchLogs 3.7.2.39

Targeted .NET Platform

4.8

Operating System and version

Windows Server 2016 Datacenter

@lanierhall lanierhall added bug This issue is a bug. needs-triage This issue or PR still needs to be triaged. labels Apr 14, 2022
@lanierhall
Copy link
Author

lanierhall commented Apr 14, 2022

PR #200 is a fix for this and we've run it for over two years in our system without any issues.
#200

@ashishdhingra
Copy link
Contributor

Thanks for submitting the PR. This needs review from team.

@ashishdhingra ashishdhingra added pr/needs-review This PR needs a review from a Member. A and removed needs-triage This issue or PR still needs to be triaged. labels Apr 14, 2022
@normj
Copy link
Member

normj commented Apr 15, 2022

@lanierhall I'm trying to replicate the AWSLoggerCore getting created multiple times and I have not been successful yet. I have removed my shared credentials so I'm sure the only place credentials are coming from is my static credentials in app.config. My code looks like this and I can verify credentials are getting recorded to CloudWatch Logs and the AWSLoggerCore constructor is only be called. Can you help understand what I'm not doing correctly?

[assembly: log4net.Config.XmlConfigurator(ConfigFile = "log4net.config")]

namespace LoggingAppSettingstEST
{
    internal class Program
    {
        private static readonly ILog Logger = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);

        static void Main(string[] args)
        {
            try
            {
                AWSConfigs.LoggingConfig.LogTo = LoggingOptions.Log4Net;
                AWSConfigs.LoggingConfig.LogResponses = ResponseLoggingOption.Always;


                Logger.Debug("Info Hello Log");
                Logger.Error("Error Hello Log");

                var s3Client = new AmazonS3Client();
                s3Client.ListBuckets();

                Thread.Sleep(5000);

            }
            finally
            {
                Console.WriteLine("Hello");
            }
        }
    }
}

@lanierhall
Copy link
Author

Can you put a breakpoint on StartMonitor() in the constructor of AWSLoggerCore (AWS.Logger.Core.AWSLoggerCore.cs: line 80) and share your call stack? I'd be interested to compare it with this one:

image

@ashishdhingra ashishdhingra added p2 This is a standard priority issue and removed pr/needs-review This PR needs a review from a Member. A labels Nov 1, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug This issue is a bug. module/logging p2 This is a standard priority issue queued
Projects
None yet
Development

No branches or pull requests

4 participants