简体   繁体   English

如何使用带有Seri​​log的postharp来区分日志记录方面?

[英]How to use postsharp with Serilog to separate logging aspect?

I'm a beginner to AOP and I try to use PostSharp with SeriLog to log my MVC application . 我是AOP的初学者,我尝试将PostSharpSeriLog一起使用来记录我的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! 在业务类QueueProcessor中 ,那么方面的价值是什么! 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 ? 有人可以帮我使用PostSharp.Patterns.Diagnostics.Backends.Serilog将MVC项目的注销分开吗?

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) . 您发现了一个显而易见的activity.Write(xxx)activity.Write(xxx) This kind of logging is not covered by using an AOP framework like PostSharp. 使用诸如PostSharp之类的AOP框架不会涵盖此类日志记录。 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 : 只需在program.cs中执行以下操作,即可通过PostSharp注入此管道代码:

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? 有人可以帮助我使用PostSharp.Patterns.Diagnostics.Backends.Serilog将MVC项目的注销分开吗?

I am not sure what you expect from any AOP framework regarding custom logging inside business logic code. 我不确定您对任何AOP框架对业务逻辑代码中的自定义日志记录会有什么期望。 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. 我认为PostSharp的示例不是DDD的示例。 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. 它们仅表明用于方面的记录器也可以用于记录与业务相关的信息(将实体标记为删除)。在DDD中,如果此信息与某人或某物相关,则可能是可以使用方面记录的事件。
  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. 在这种情况下,无需使用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. 根据DDD应用程序中的事件代码,您也许能够在事件发送之前或之后也能够拦截事件,因此您也可以在其中编写自己的日志记录,而无需使用PostSharp。
  3. Did you try the steps listened in the answers of the question that has been marked as the duplicate? 您是否尝试过将已标记为重复的问题的答案中的步骤听完了?

When you're using the LogAttribute aspect, PostSharp automatically generates code that emits log records before and after the execution of a method. 使用LogAttribute方面时,PostSharp会自动生成在执行方法之前和之后发出日志记录的代码。 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. 对于这些情况,可以使用Logger类提供的方法。

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 首先,您必须从这里下载: https : //www.postsharp.net/download

When you install the plugin, create a project in visual studio. 安装插件时,请在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. 在解决方案资源管理器中,通过右键单击项目或代码文件中,通过右键单击类或方法名称,可以将PostSharp添加到项目中。

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/logging

https://doc.postsharp.net/serilog https://doc.postsharp.net/serilog

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM