TECHNOLOGIES
FORUMS
JOBS
BOOKS
EVENTS
INTERVIEWS
Live
MORE
LEARN
Training
CAREER
MEMBERS
VIDEOS
NEWS
BLOGS
Sign Up
Login
No unread comment.
View All Comments
No unread message.
View All Messages
No unread notification.
View All Notifications
Answers
Post
An Article
A Blog
A News
A Video
An EBook
An Interview Question
Ask Question
Forums
Monthly Leaders
Forum guidelines
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
{
"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;
}
}
Reply
Answers (
1
)
Set Data in TempData.
Face Null Value from Model