1

I'm a beginner to AOP and I try to use PostSharp with SeriLog to log my MVC application.

So I find this sample example as a start, but I wonder If in this example it uses the logger explicitly like that:

activity.Write(LogLevel.Warning, "The entity {id} has been marked for deletion.", item.Id);

in a business class QueueProcessor, Then what's the value of aspect here! I still write logging code coupled with business code!.


Could someone help me to separate the logging out of the MVC project using PostSharp.Patterns.Diagnostics.Backends.Serilog?

Anyname Donotcare
  • 11,113
  • 66
  • 219
  • 392

2 Answers2

3

You are misunderstanding the example, let me clearify.

There are two kinds of logging in the sample code. You found the obvious one, activity.Write(xxx). This kind of logging is not covered by using an AOP framework like PostSharp. This kind of logging can be seen as part of the business logic as it is specific to the actions that are taking place.

Now, on the other hand: let's assume you want to log each and every call to all methods in your application. You want to log when the method is called and with what parameters. Also, you want to log the returned value, if any.

Imaging having to write something like this for every method:

bool SomeMethod(int input)
{
    var sw = Stopwatch.StartNew();
    logger.Write($"Started executing {nameof(SomeMethod)} at {DateTime.Now}. Value of {nameof(input)}: {input})";

    ... // some work
    var returnValue = false;

    sw.Stop();
    logger.Write($"Finished executing {nameof(SomeMethod)}. It took {sw.ElapsedMilliseconds}ms. Returned value: {returnValue}");

    return returnValue;
}

Now that is a cross cutting concern and that is what the example demonstrates. This plumbing code is injected by PostSharp by just doing this in program.cs:

using PostSharp.Patterns.Diagnostics;
using PostSharp.Patterns.Diagnostics.Backends.Serilog;
using PostSharp.Samples.Logging.BusinessLogic;
using Serilog;

// Add logging to all methods of this project.
[assembly: Log]
    ...

More details here

Now, at the end, let's go back to your question:

Could someone help me to separate the logging out of the MVC project using PostSharp.Patterns.Diagnostics.Backends.Serilog?

I am not sure what you expect from any AOP framework regarding custom logging inside business logic code. Could you expand on this? Or is the above clarification enough?

Edit: addressing the issues from your comment

  1. I do not think the example of PostSharp is a DDD example. They merely demonstrate that the logger used for the aspect can also be used to log business related information (Entity is marked for deletion.) In DDD if this information is relevant to someone or something it would be an event that could be logged using an aspect.
  2. Creating an audit trail by using auditing aspects for capturing the events is certainly the right way to do that. As to the presentation layer, you could use some middleware to log requests and responses like demonstrated here for example. In that case there is no need to use PostSharp. Depending on your eventing code in your DDD application you might be able to intercept events as well before or after they are send so you can write your own logging there as well, removing the need for PostSharp.
  3. Did you try the steps listened in the answers of the question that has been marked as the duplicate?
Peter Bons
  • 26,826
  • 4
  • 50
  • 74
  • Thanks a lot but I have three questions: 1- `"This kind of logging can be seen as part of the business logic as it is specific to the actions that are taking place."` How to consider `Log` as a part of my business ?, It's not a part of my business, I follow `DDD` so I have to isolate my domain away of any framework or any aspects, so I expect to log the action methods inners through postsharp. 2-I target the `MVC` (presentation layer) by logging aspects and I target the `Domain layer` by Auditing aspects, am I on the right track ? – Anyname Donotcare Nov 29 '18 at 08:31
  • 3-I try to isolate the logging aspects and the needed configuration it its own class library but i get the following error : https://stackoverflow.com/q/53396131/418343 – Anyname Donotcare Nov 29 '18 at 08:31
  • `Did you try the steps listened in the answers of the question that has been marked as the duplicate? ` yes I did but in vain – Anyname Donotcare Dec 02 '18 at 09:49
  • Could you summarize your ideas, How can I begin to log and audit my application (DDD) using `AOP` please – Anyname Donotcare Dec 02 '18 at 09:50
  • Are you using any ddd frameworks in your code? What are you using as the event bus. Mediatr? – Peter Bons Dec 02 '18 at 09:53
  • Could I ask what do U mean by DDD frameworks please ? – Anyname Donotcare Dec 02 '18 at 09:55
  • I Just use EF6 For Data layer and .net MVC for my presentation layer and I want to use postsharp for any aspects(Logging and auditing) – Anyname Donotcare Dec 02 '18 at 09:56
  • Wel, you are probably passing domain events around that are picked up by handlers. Have you coded that yourself? Would it suffice to log the events for auditing purposes. – Peter Bons Dec 02 '18 at 10:37
  • Yes, i did it myself, could u explain what do you mean by ddd frameworks please – Anyname Donotcare Dec 02 '18 at 13:12
  • Well, take a look at an example architecture like [this one](https://github.com/JasonGT/NorthwindTraders). It uses MediatR to pass domain events and it provides nice extension points t log create an audit log based on those messages. – Peter Bons Dec 03 '18 at 07:27
  • We are getting a bit out of scope of the original question, summarized: yes, you could use aspects for both auditing and application logging, but their might be other options as well. – Peter Bons Dec 03 '18 at 07:29
2

When you're using the LogAttribute aspect, PostSharp automatically generates code that emits log records before and after the execution of a method. But there are times when you will want to write your own records. For instance, you may want to log a custom error or warning. You may want this message to be displayed even when trace-level logging is disabled. But when it is enabled, you want this message to appear in the right context, with the proper indentation. For these scenarios, you can use the methods provided by the Logger class.

In the sample class, some custom logs are added manually about the business logic.


Firstly, you have to download it from here: https://www.postsharp.net/download

When you install the plugin, create a project in visual studio. In solution explorer, by right clicking the project or in your code file, by right clicking the class or method name, you can add PostSharp to your project.

The default configuration and attributes are added in the project. After that, you will change configurations, formatting, maybe add custom aspect classes base on your needs.

To continue with reading these documentations will be useful:

https://doc.postsharp.net/logging

https://doc.postsharp.net/serilog

Django
  • 222
  • 1
  • 5