Vinay
Vinay

Reputation: 285

Can we directly log to Cloudwatch when running .net application in Visual Studio

I have implemented Serilog logging in my .net application. I wanted to know if i can directly post logs into AWS Cloud watch. If yes what are the steps i need to take. How and where should i configure and save AWS Credentials. (I am not hosting my application in Ec2 or Elastic Beanstalk).

I have referred below link and few other links and done exactly as suggested, But nothing is logged in Cloud watch. The link also does not suggest how i should give AWS credentials from my Windows System from where i am running the application in Visual Studio.

How to configure a Serilog sink for logging to CloudWatch

Upvotes: 2

Views: 2036

Answers (1)

Mahdi
Mahdi

Reputation: 3349

You can use NLog.AWS.Logger package (among others) from Nuget. For configuring it you have two options: via config file or through your code like this:

public static async Task Main(string[] args)
{
    ConfigureNLog();
    Logger logger = LogManager.GetCurrentClassLogger();
    logger.Info("my test log entry");
}

static void ConfigureNLog()
{
    var config = new LoggingConfiguration();
    var consoleTarget = new ColoredConsoleTarget();
    config.AddTarget("console", consoleTarget);
    var awsTarget = new AWSTarget()
    {
        LogGroup = "NLog.ProgrammaticConfigurationExample",
        Region = "eu-west-1"
    };
    config.AddTarget("aws", awsTarget);
    config.LoggingRules.Add(new LoggingRule("*", LogLevel.Debug, consoleTarget));
    config.LoggingRules.Add(new LoggingRule("*", LogLevel.Debug, awsTarget));
    LogManager.Configuration = config;
}

According to the documentation, the AWS credentials are found using the standard AWS SDK for .NET credentials search path. You can set your credentials using PowerShell like follows:

PS > Set-AWSCredential `
             -AccessKey [YOUR ACCESS KEY without brackets] `
             -SecretKey [YOUR SECRET KEY without brackets] `
             -StoreAs default

Upvotes: 3

Related Questions