|
| 1 | +using Microsoft.AspNetCore.Hosting; |
| 2 | +using Microsoft.Extensions.Configuration; |
| 3 | +using Microsoft.Extensions.Hosting; |
| 4 | +using Microsoft.Extensions.Logging; |
| 5 | +using System; |
| 6 | +using System.Collections.Generic; |
| 7 | +using System.Diagnostics; |
| 8 | +using System.Linq; |
| 9 | +using System.Threading.Tasks; |
| 10 | + |
| 11 | +namespace AspNetCoreMsLoggerExample.Web |
| 12 | +{ |
| 13 | + public class Program |
| 14 | + { |
| 15 | + public static void Main(string[] args) |
| 16 | + { |
| 17 | + try |
| 18 | + { |
| 19 | + using IHost host = CreateHostBuilder(args).Build(); |
| 20 | + host.Run(); |
| 21 | + } |
| 22 | + catch (Exception ex) |
| 23 | + { |
| 24 | + // Loading configuration or running the application failed. |
| 25 | + // This will create a logger that can be captured by Azure logger. |
| 26 | + // To enable Azure logger, in Azure Portal: |
| 27 | + // 1. Go to WebApp |
| 28 | + // 2. App Service logs |
| 29 | + // 3. Enable "Application Logging (Filesystem)", "Application Logging (Filesystem)" and "Detailed error messages" |
| 30 | + // 4. Set Retention Period (Days) to 10 or similar value |
| 31 | + // 5. Save settings |
| 32 | + // 6. Under Overview, restart web app |
| 33 | + // 7. Go to Log Stream and observe the logs |
| 34 | + Console.WriteLine("Host terminated unexpectedly: " + ex); |
| 35 | + } |
| 36 | + } |
| 37 | + |
| 38 | + public static IHostBuilder CreateHostBuilder(string[] args) |
| 39 | + => Host.CreateDefaultBuilder(args) |
| 40 | + .ConfigureWebHostDefaults(webBuilder => |
| 41 | + { |
| 42 | + webBuilder.UseStartup<Startup>() |
| 43 | + .CaptureStartupErrors(true) |
| 44 | + .ConfigureAppConfiguration(config => |
| 45 | + { |
| 46 | + config |
| 47 | + // Used for local settings like connection strings. |
| 48 | + .AddJsonFile("appsettings.Local.json", optional: true); |
| 49 | + }) |
| 50 | + .ConfigureLogging((hostingContext, loggingBuilder) => { |
| 51 | +#if DEBUG |
| 52 | + // Add Seq for local dev. |
| 53 | + loggingBuilder.AddSeq(); |
| 54 | +#endif |
| 55 | + }); |
| 56 | + }); |
| 57 | + } |
| 58 | +} |
0 commit comments