Created
May 13, 2021 20:34
-
-
Save manoj-choudhari-git/09887094cbacbc155f73c9a0f6681db8 to your computer and use it in GitHub Desktop.
.NET Web Application - Logging - Add Azure App Service Diagnostics and Application Insights Logging Providers
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Program.cs for a web application | |
public class Program | |
{ | |
public static void Main(string[] args) | |
{ | |
CreateHostBuilder(args).Build().Run(); | |
} | |
public static IHostBuilder CreateHostBuilder(string[] args) => | |
Host.CreateDefaultBuilder(args) | |
.ConfigureWebHostDefaults(webBuilder => | |
{ | |
webBuilder.UseStartup<Startup>(); | |
webBuilder.ConfigureLogging((hostingContext, loggingBuilder) => | |
{ | |
loggingBuilder.ClearProviders(); | |
loggingBuilder.AddJsonConsole(options => | |
{ | |
options.IncludeScopes = false; | |
options.TimestampFormat = "hh:mm:ss "; | |
options.JsonWriterOptions = new JsonWriterOptions | |
{ | |
Indented = true | |
}; | |
}); | |
// Application Insights, just add instrumentation key inside double quotes | |
loggingBuilder.AddApplicationInsights(instrumentationKey: ""); | |
// Azure App Service Diagnostics | |
loggingBuilder.AddAzureWebAppDiagnostics(); | |
}); | |
}); | |
} | |
// Startup.cs | |
public class Startup | |
{ | |
public void ConfigureServices(IServiceCollection services) | |
{ | |
// Azure App Service File Logger Options | |
services.Configure<AzureFileLoggerOptions>(options => | |
{ | |
options.FileName = "azure-diagnostics-"; | |
options.FileSizeLimit = 50 * 1024; | |
options.RetainedFileCountLimit = 5; | |
}); | |
// Azure App Service Storage Account Blob Logger Options | |
services.Configure<AzureBlobLoggerOptions>(options => | |
{ | |
options.BlobName = "log.txt"; | |
}); | |
services.AddControllersWithViews(); | |
} | |
// Some other code | |
public void Configure(IApplicationBuilder app, IWebHostEnvironment env) | |
{ | |
// Some Code.... | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment