logging in the application using Serilog

289 views Asked by At

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

0

There are 0 answers