Aspect-oriented programming in .NET Core
Step 3. Create the class file under an “Interface” folder, then setting the “ILogRepository.”
namespace Enterprise_Dot_Net_Core_WebApp.Logging.Interface
{
public interface ILogRepository
{
}
}
Step 4. Create the class file under a “Repository” folder, then set the “LogRepository.” Add the action filter source code in the LogRepository file.

public override void OnActionExecuting(ActionExecutingContext context)
{
_logger.LogInformation("ClassFilter OnActionExecuting");
base.OnActionExecuting(context);
}
public override void OnActionExecuted(ActionExecutedContext context)
{
_logger.LogInformation("ClassFilter OnActionExecuted");
base.OnActionExecuted(context);
}
public override void OnResultExecuting(ResultExecutingContext context)
{
_logger.LogInformation("ClassFilter OnResultExecuting");
if (context.Result != null)
{
var resultMsg = context.Result;
if (resultMsg is JsonResult json)
{
_logger.LogInformation("JSON Serializer Setting: " + json.SerializerSettings);
_logger.LogInformation("JSON Content Type: " + json.ContentType);
_logger.LogInformation("JSON Status Code: " + json.StatusCode);
_logger.LogInformation("JSON Value: " + JsonConvert.SerializeObject(json.Value));
}
if (resultMsg is ViewResult view)
{
_logger.LogInformation("View Name: " + view.ViewName);
_logger.LogInformation("View Status: " + view.StatusCode);
_logger.LogInformation("View ContentTpye:" + view.ContentType);
_logger.LogInformation("View Data: " + JsonConvert.SerializeObject(view.ViewData));
_logger.LogInformation("View Data Model: " + JsonConvert.SerializeObject(view.ViewData.Model));
_logger.LogInformation("Temp Data: " + JsonConvert.SerializeObject(view.TempData));
}
}
base.OnResultExecuting(context);
}
public override void OnResultExecuted(ResultExecutedContext context)
{
_logger.LogInformation("ClassFilter OnResultExecuted");
base.OnResultExecuted(context);
}
Step 5. Install Serilog.Extensions.Logging.File NuGet package by NuGet management platform in Visual Studio 2017.

2 Comments