0

Unable to fetch Serilog > SourceContext property value using code. SourceContext property is not getting generated against Properties field in log table. 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.

Using the below lines of code

appsetting.json

{  
"Serilog": {  
    "MinimumLevel": {  
        "Default": "Information",  
        "Override": {  
            "Microsoft": "Warning",  
            "System": "Warning"  
        }  
    },  
    "Enrich": [ "WithOpenTracingContext", "FromLogContext", "WithMachineName", "WithProcessId", "WithThreadId", "WithCorrelationId" ],  
    "WriteTo": [  
        {  
            "Name": "MSSqlServer",  
            "Args": {  



                "connectionString": "connection string",  
                "autoCreateSqlTable ": true,  
                "schemaName": "dbo",  
                "tableName": "LogTable",  
                "restrictedToMinimumLevel": "Information",  
                "batchPostingLimit": 50,  
                "period": "0.00:00:30",  
                "columnOptionsSection": {  
                    "removeStandardColumns": [ "LogEvent", "MessageTemplate" ],  
                    "customColumns": [  
                        {  
                            "ColumnName": "ClientIp",  
                            "DataType": "VARCHAR",  
                            "AllowNull": true,  
                            "DataLength": 50,  
                            "NonClusteredIndex": true  
                        },  
                        {  
                            "ColumnName": "UserId",  
                            "DataType": "INT",  
                            "AllowNull": true  
                        },  
                        {  
                            "ColumnName": "SessionId",  
                            "DataType": "VARCHAR",  
                            "AllowNull": true,  
                            "DataLength": 50,  
                            "NonClusteredIndex": true  
                        }  
                    ]  
                }  
            }  
        }  
    ],  
    "Using": [ "Serilog.Settings.Configuration" ]  
}  

configuration.cs

public static IConfigurationRoot ConfigureSqlLogger(this IConfigurationRoot _configuration)  
    {  
        LoggerConfiguration config =   
            new LoggerConfiguration()  
                .Enrich.WithClientIp()  
                .Enrich.WithClientAgent()    
                  
                .ReadFrom.Configuration(_configuration);  

        Log.Logger = config.CreateLogger();  
        return _configuration;  
    }  

Startup.cs

public Startup(IWebHostEnvironment environment)  
   {  
       try  
       {  
           var builder = new ConfigurationBuilder()  
           .SetBasePath(environment.ContentRootPath)  
           .AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)  
           .AddJsonFile($"appsettings.{environment.EnvironmentName}.json", optional: true)  
           .AddEnvironmentVariables();  
           Configuration = builder.Build();  

           Configuration.ConfigureSqlLogger();  
       }  
       catch (Exception ex)  
       {  
           throw ex;  
       }  
   }  

enter image description here

enter image description here

James Z
  • 12,209
  • 10
  • 24
  • 44
  • 1
    Not directly related to your stated issue, but still something you should fix: never do `throw ex`; where `ex` is a caught exception. This causes you to lose the stack trace, and is very painful when debugging. Instead, you should simply `throw;`. Or in this case since the catch block isn't doing anything useful, you can remove the whole try/catch. – mason May 27 '21 at 20:32
  • How are you logging events? Are you using Microsoft.Extensions.Logging in your classes? Or are your classes logging directly to Serilog? – mason May 27 '21 at 20:38

0 Answers0