@@ -54,3 +54,89 @@ In your application's `App.config` or `Web.config` file, specify the file sink a
5454### Async approach
5555It is possible to configure the collector using [ Serilog.Sinks.Async] ( https://github.com/serilog/serilog-sinks-async ) to have an async approach.
5656
57+ ### Example Configuration for ASP.NET
58+
59+ ``` c#
60+ public static class Program
61+ {
62+ private const string OutputTemplate = " {Timestamp:HH:mm:ss.fff} [{Level:u1}] {Message:lj}{NewLine}{Exception}" ;
63+
64+ public static async Task <int > Main (string [] args )
65+ {
66+ // azure home directory
67+ var homeDirectory = Environment .GetEnvironmentVariable (" HOME" ) ?? " ." ;
68+ var logDirectory = Path .Combine (homeDirectory , " LogFiles" );
69+
70+ Log .Logger = new LoggerConfiguration ()
71+ .MinimumLevel .Verbose ()
72+ .MinimumLevel .Override (" Microsoft" , LogEventLevel .Information )
73+ .Enrich .FromLogContext ()
74+ .WriteTo .Console (outputTemplate : OutputTemplate )
75+ .WriteTo .File (
76+ path : $" {logDirectory }/boot.txt" ,
77+ rollingInterval : RollingInterval .Day ,
78+ shared : true ,
79+ flushToDiskInterval : TimeSpan .FromSeconds (1 ),
80+ outputTemplate : OutputTemplate ,
81+ retainedFileCountLimit : 10
82+ )
83+ .CreateBootstrapLogger ();
84+
85+ try
86+ {
87+ Log .Information (" Starting web host" );
88+
89+ var builder = Microsoft .AspNetCore .Builder .WebApplication .CreateBuilder (args );
90+
91+ builder .Host
92+ .UseSerilog ((context , services , configuration ) => configuration
93+ .ReadFrom .Configuration (context .Configuration )
94+ .ReadFrom .Services (services )
95+ .Enrich .FromLogContext ()
96+ .Enrich .WithProperty (" ApplicationName" , builder .Environment .ApplicationName )
97+ .Enrich .WithProperty (" EnvironmentName" , builder .Environment .EnvironmentName )
98+ .WriteTo .Console (outputTemplate : OutputTemplate )
99+ .WriteTo .File (
100+ path : $" {logDirectory }/log.txt" ,
101+ rollingInterval : RollingInterval .Day ,
102+ shared : true ,
103+ flushToDiskInterval : TimeSpan .FromSeconds (1 ),
104+ outputTemplate : OutputTemplate ,
105+ retainedFileCountLimit : 10
106+ )
107+ .WriteTo .AzureTableStorage (
108+ connectionString : context .Configuration .GetConnectionString (" StorageAccount" ),
109+ propertyColumns : new [] { " SourceContext" , " RequestId" , " RequestPath" , " ConnectionId" , " ApplicationName" , " EnvironmentName" }
110+ )
111+ );
112+
113+ ConfigureServices (builder );
114+
115+ var app = builder .Build ();
116+
117+ ConfigureMiddleware (app );
118+
119+ await app .RunAsync ();
120+
121+ return 0 ;
122+ }
123+ catch (Exception ex )
124+ {
125+ Log .Fatal (ex , " Host terminated unexpectedly" );
126+ return 1 ;
127+ }
128+ finally
129+ {
130+ await Log .CloseAndFlushAsync ();
131+ }
132+ }
133+
134+ private static void ConfigureServices (WebApplicationBuilder builder )
135+ {
136+ }
137+
138+ private static void ConfigureMiddleware (Microsoft.AspNetCore.Builder.WebApplication app )
139+ {
140+ }
141+ }
142+ ```
0 commit comments