G Y

G Y

  • 1.5k
  • 229
  • 43.9k

logging in the application using Serilog

May 25 2021 3:03 PM
 
 1. Unable to fetch Serilog > SourceContext property value using code

 2. SourceContext property is not getting generated against Properties field in log table.
 
 3. Trying to fetch SourceContext value of every method i.e., api, service, repository by using a common method globally instead of fetching SourceContext value in every method. 
 
Please suggest 
 
Using the below lines of code
 
appsetting.json
  1. {  
  2.     "Serilog": {  
  3.         "MinimumLevel": {  
  4.             "Default""Information",  
  5.             "Override": {  
  6.                 "Microsoft""Warning",  
  7.                 "System""Warning"  
  8.             }  
  9.         },  
  10.         "Enrich": [ "WithOpenTracingContext""FromLogContext""WithMachineName""WithProcessId""WithThreadId""WithCorrelationId" ],  
  11.         "WriteTo": [  
  12.             {  
  13.                 "Name""MSSqlServer",  
  14.                 "Args": {  



  15.                     "connectionString""connection string",  
  16.                     "autoCreateSqlTable "true,  
  17.                     "schemaName""dbo",  
  18.                     "tableName""LogTable",  
  19.                     "restrictedToMinimumLevel""Information",  
  20.                     "batchPostingLimit": 50,  
  21.                     "period""0.00:00:30",  
  22.                     "columnOptionsSection": {  
  23.                         "removeStandardColumns": [ "LogEvent""MessageTemplate" ],  
  24.                         "customColumns": [  
  25.                             {  
  26.                                 "ColumnName""ClientIp",  
  27.                                 "DataType""VARCHAR",  
  28.                                 "AllowNull"true,  
  29.                                 "DataLength": 50,  
  30.                                 "NonClusteredIndex"true  
  31.                             },  
  32.                             {  
  33.                                 "ColumnName""UserId",  
  34.                                 "DataType""INT",  
  35.                                 "AllowNull"true  
  36.                             },  
  37.                             {  
  38.                                 "ColumnName""SessionId",  
  39.                                 "DataType""VARCHAR",  
  40.                                 "AllowNull"true,  
  41.                                 "DataLength": 50,  
  42.                                 "NonClusteredIndex"true  
  43.                             }  
  44.                         ]  
  45.                     }  
  46.                 }  
  47.             }  
  48.         ],  
  49.         "Using": [ "Serilog.Settings.Configuration" ]  
  50. }  
configuration.cs
  1. public static IConfigurationRoot ConfigureSqlLogger(this IConfigurationRoot _configuration)  
  2. {  
  3.     LoggerConfiguration config =   
  4.         new LoggerConfiguration()  
  5.             .Enrich.WithClientIp()  
  6.             .Enrich.WithClientAgent()    
  7.                       
  8.             .ReadFrom.Configuration(_configuration);  
  9.   
  10.     Log.Logger = config.CreateLogger();  
  11.     return _configuration;  
  12. }  
Startup.cs
  1. public Startup(IWebHostEnvironment environment)  
  2. {  
  3.     try  
  4.     {  
  5.         var builder = new ConfigurationBuilder()  
  6.            .SetBasePath(environment.ContentRootPath)  
  7.            .AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)  
  8.            .AddJsonFile($"appsettings.{environment.EnvironmentName}.json", optional: true)  
  9.            .AddEnvironmentVariables();  
  10.         Configuration = builder.Build();  
  11.   
  12.         Configuration.ConfigureSqlLogger();  
  13.     }  
  14.     catch (Exception ex)  
  15.     {  
  16.         throw ex;  
  17.     }  
  18. }  
 
 

Answers (1)