دسته بندی کردن لاگها در Serilog
نویسنده: زمانی فرهاد
تاریخ: ۱۳۹۹/۰۷/۲۹ ۲۳:۳۰
آدرس: www.dntips.ir
| مطالب | ۳۶۹۴ |
| نویسندگان | ۲۷۶ |
| گروههای مطالب | ۱۰۲۴ |
| نقشههای راه | ۱۱۹ |
| دورهها | ۱۴ |
| اشتراکها | ۱۷۹۱۴ |
public IActionResult ConfirmOrder(Order order)
{
using (Serilog.Context.LogContext.PushProperty("OrderId", order.Id))
{
_logger.LogInformation("Check order validation");
//DoSomething
_logger.LogInformation("Order validation successfully");
//DoSomething
_orderService.ConfirmOrder(order);
_logger.LogInformation("Order confirmed successfully");
}
return Ok();
} {
"Timestamp":"2020-10-20T23:01:01.0492132+03:30",
"Level":"Information",
"MessageTemplate":"Order Confirmed successfully",
"Properties":{
"SourceContext":"SerilogExamlpe.WebApplication.Controllers.WeatherForecastController",
"ActionId":"870582be-312f-4065-88eb-5675e2df4928",
"ActionName":"SerilogExamlpe.WebApplication.Controllers.WeatherForecastController.Get (SerilogExamlpe.WebApplication)",
"RequestId":"0HM3L5QM34E6K:00000001",
"RequestPath":"/weatherforecast",
"SpanId":"|da92fcac-4169ab4e937de2ae.",
"TraceId":"da92fcac-4169ab4e937de2ae",
"ParentId":"",
"OrderId":12345,//<-- NOTE THIS
"MachineName":"FARHAD-PC",
"Environment":"Development"
}
} using (_logger.BeginScope("OrderId : {orderId}", 12345)) app.Use(async (httpContext, next) =>
{
//Get username
var username = httpContext.User.Identity.IsAuthenticated ? httpContext.User.Identity.Name : "anonymous";
LogContext.PushProperty("User", username);
//Get remote IP address
var ip = httpContext.Connection.RemoteIpAddress.ToString();
LogContext.PushProperty("IP", !String.IsNullOrWhiteSpace(ip) ? ip : "unknown");
await next.Invoke();
}); app.Use(async (httpContext, next) =>
{
//Get username
var username = httpContext.User.Identity.IsAuthenticated ? httpContext.User.Identity.Name : "anonymous";
using (LogContext.PushProperty("User", username))
{
//Get remote IP address
var ip = httpContext.Connection.RemoteIpAddress.ToString();
using (LogContext.PushProperty("IP", !String.IsNullOrWhiteSpace(ip) ? ip : "unknown"))
{
await next.Invoke();
}
}
});