Categories: ASP.NETASP.NET Core

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.

Fig 3. Add “Before” & “After” source code.
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.

Fig 4 Install Serilog.Extensions.Logging.File NuGet package

Page: 1 2 3 4 5

davidsky69

View Comments

Recent Posts

SOLID Principles – Single-responsibility principle

The single-responsibility principle (SRP) is a computer-programming principle that states that every module, class or…

4 years ago

Microservices – Command and Query Responsibility Segregation(CQRS)

Command query responsibility segregation (CQRS) generalizes CQS to message-driven and event-driven architectures: it applies the CQS…

4 years ago

Microservices – Domain-driven design(DDD)

Domain-driven design (DDD) is a software design approach focusing on modelling software to match a…

4 years ago

Microservices – Onion architecture

A microservice architecture – a variant of the service-oriented architecture (SOA) structural style – arranges…

4 years ago

Unit Of Work pattern concepts in .NET

The Repository pattern and Unit of Work pattern are used together in most time. Unit…

4 years ago

Behavioral Patterns – Visitor

Visitor – It allows the program changes the algorithm and the operations that base on the visitor…

4 years ago