Sushrut Paranjape
Sushrut Paranjape

Reputation: 547

Console application Logs to store in Azure storage

I am using a console application that runs on on-premise servers triggered by a task scheduler. This console application performs the various actions and needed to be logged these. It would generate logs of around 200kb per run and the console app runs every hour.

Since the server is not accessible to us, I am planning to store the logs to Azure. I read about blob/table storage.

I would like to know what is the best strategy to store the logs in Azure.

Thank you.

Upvotes: 0

Views: 458

Answers (1)

Gaurav Mantri
Gaurav Mantri

Reputation: 136196

Though you can write logging data in Azure Storage (both Blobs and Tables), it would actually make more sense if you use Azure Application Insights for logging this data.

I recently did the same for a console application I built. I found it incredibly simple.

I created an App Insight Resource in my Azure Subscription and got the instrumentation key. I then installed App Insights SDK and referenced appropriate namespaces in my project.

using Microsoft.ApplicationInsights;
using Microsoft.ApplicationInsights.DataContracts;
using Microsoft.ApplicationInsights.Extensibility;

This is how I initialized the telemetry client:

                var appSettingsReader = new AppSettingsReader();
                var appInsightsInstrumentationKey = (string)appSettingsReader.GetValue("AppInsights.InstrumentationKey", typeof(string));

                TelemetryConfiguration configuration = TelemetryConfiguration.CreateDefault();
                configuration.InstrumentationKey = appInsightsInstrumentationKey;
                telemetryClient = new TelemetryClient(configuration);
                telemetryClient.InstrumentationKey = appInsightsInstrumentationKey;

For logging trace data, I simply did the following:

                            TraceTelemetry telemetry = new TraceTelemetry(message, SeverityLevel.Verbose);
                            telemetryClient.TrackTrace(telemetry);

For logging error data, I simply did the following:

            catch (Exception excep)
            {
                var message = string.Format("Error. {0}", excep.Message);
                ExceptionTelemetry exceptionTelemetry = new ExceptionTelemetry(excep);
                telemetryClient.TrackException(exceptionTelemetry);

                telemetryClient.Flush();
                Task.Delay(5000).Wait();//Wait for 5 seconds before terminating the application
            }

Just keep one thing in mind though: Make sure you wait for some time (5 seconds is good enough) to flush the data before terminating the application.


If you're still keen on writing logs to Azure Storage, depending on the logging library you're using you will find suitable adapters that will write directly into Azure Storage.

For example, there's an NLog target for Azure Tables: https://github.com/harouny/NLog.Extensions.AzureTableStorage (though this project is not actively maintained).

Upvotes: 3

Related Questions