1. 如何在 ASP.Net Core 中使用 LoggerMessage

        共 2225字,需瀏覽 5分鐘

         ·

        2021-01-29 19:36

        ASP.NET Core 是一個開源的、跨平臺的、輕量級模塊化框架,可用于構建高性能、可伸縮的web應用程序,你也許不知道 ASP.NET Core 中有一個藏得很深,而且非常強大的特性,那就是 LoggerMessage,與內建的 Logger 相比,前者具有更高的性能,這篇文章我們來一起討論 LoggerMessage 到底能帶來什么好處,以及如何在 ASP.NET Core 3.0 中使用 LoggerMessage 。

        LoggerMessage VS Logger

        與內置的 Logger 相比,LoggerMessage提供了以下幾個優(yōu)點。

        • 性能

        LoggerMessage 比 Logger 具有更少的對象分配和計算開銷,內建的 Logger 有裝箱操作,而 LoggerMessage 巧妙的利用了靜態(tài)字段,靜態(tài)方法,以及具有強類型擴展方法來避免裝箱開銷。

        • 解析

        Logger 相比,LoggerMessage 的解析機制更加高效,Logger 會在每次寫入消息的時候都要解析模板,而 LoggerMessage 只需在消息定義的時候解析一次。

        使用 LoggerMessage.Define 方法

        Microsoft.Extensions.Logging 命名空間下的 LoggerMessage.Define 方法可用于高性能的記錄日志,要使用這個方法,需要指定正確的強類型參數(shù)。

        下面是 LoggerHelper.Define() 源碼定義

        接下來我們看一下如何使用 LoggerMessage.Define 方法,先定義一個靜態(tài)的 LoggerExtensions 類,如下代碼所示:


        internal?static?class?LoggerExtensions
        {

        }

        接下來創(chuàng)建一個用來記錄日志的擴展方法,內部使用的是 LoggerMessage.Define 方法,代碼如下:


        ????internal?static?class?LoggerExtensions
        ????{
        ???????public?static?void?RecordNotFound(this?ILogger?logger,?int?id)?=>?NotFound(logger,?id,?null);
        ???????
        ???????private?static?readonly?Actionint,?Exception>?NotFound?=?LoggerMessage.Define<int>?(LogLevel.Error,?new?EventId(1234,?nameof(NotFound)),"The?record?is?not?found:?{Id}");
        ????}

        Action 中使用 LoggerMessage

        接下來在項目默認的 HomeController.Index() 方法中使用剛才創(chuàng)建的日志擴展方法,如下代碼所示:


        ????public?class?HomeController?:?Controller
        ????{
        ????????private?readonly?ILogger?_logger;

        ????????public?HomeController(ILogger?logger)
        ????????{
        ????????????_logger?=?logger;
        ????????}

        ????????public?IActionResult?Index()
        ????????{
        ????????????_logger.RecordNotFound(1);

        ????????????return?View();
        ????????}
        ????}

        ????internal?static?class?LoggerExtensions
        ????{
        ????????public?static?void?RecordNotFound(this?ILogger?logger,?int?id)?=>?NotFound(logger,?id,?null);

        ????????private?static?readonly?Actionint,?Exception>?NotFound?=?LoggerMessage.Define<int>(LogLevel.Error,?new?EventId(1234,?nameof(NotFound)),?"The?record?is?not?found:?{Id}");
        ????}

        LoggerMessage.Define 可以用來創(chuàng)建能夠緩沖日志消息的委托,這種方法相比內建的 Logger 具有更高的性能,最后你可以在 https://docs.microsoft.com/en-us/aspnet/core/fundamentals/logging/loggermessage?view=aspnetcore-3.1 ?上了解更多關于 LoggerMessage 的知識。

        譯文鏈接:https://www.infoworld.com/article/3535790/how-to-use-loggermessage-in-aspnet-core-30.html


        瀏覽 39
        點贊
        評論
        收藏
        分享

        手機掃一掃分享

        分享
        舉報
        評論
        圖片
        表情
        推薦
        點贊
        評論
        收藏
        分享

        手機掃一掃分享

        分享
        舉報
          
          

            1. 欧美性生交XXXXX | 一级A片60分钟免费看 | 囯产精品久久久久久久久久妞妞 | 草久久久久 | 男生女生插插 |