asp.net core系列 73 Exceptionless+Nlog以及Apollo介紹


一. 介紹

  在一上篇中介紹了Exceptionless的基本使用,這篇主要講Exceptionless結合Nlog的實現雙重日志記錄,包括ExceptionlesUI可視化日志以及Nlogtxt文件日志。再是從Apollo配置中心讀取配置文件,當系統越龐大越多時,需要配置的參數也越來越多,可以通過使用Apollo配置中心來統一管理,例如:配置數據庫連接地址、Exceptionless的對應項目的apikey值,redis連接地址等等所有可配置的參數。

 

  1.1 asp.net core中Apollo配置

    打開asp.net core 項目,刪除appsettings.json文件默認內容,在添加配置如下所示:

{
  "apollo": {
    "AppId": "100001",
    "MetaServer": "http://192.168.0.100:8080/",
    "Env": "Dev",
    "Meta": {
      "DEV": "http://192.168.0.100:8080/",
      "FAT": "http://192.168.0.100:8080/",
      "UAT": "http://192.168.0.100:8080/",
      "PRO": "http://192.168.0.100:8080/"
    }
  }
}

    appsettings.json配置對應的Apollo客戶端配置中心如下,這里端口8070是Apollo客戶端配置界面。端口8080是.net core程序讀取Apollo配置地址。Apollo配置中參數都是以key-value的形式存儲。

    下面是讀取Apollo配置文件的關鍵代碼:

      安裝包如下:

        Install-Package Microsoft.Extensions.Configuration -Version 2.2.0
        Install-Package Com.Ctrip.Framework.Apollo.Configuration -Version 2.0.3
      private static IConfigurationRoot _root = null;

        /// <summary>
        /// 獲取Apollo的config
        /// </summary>
        /// <returns></returns>
        public static IConfigurationRoot GetRoot()
        {
            if (_root != null)
            {
                return _root;
            }

            //先獲取appsettings.json的配置
            var config = new ConfigurationBuilder()
             .SetBasePath(Directory.GetCurrentDirectory())
             .AddJsonFile("appsettings.json")
             .Build();

            //連接Apollo
            string appId = config.GetSection("apollo").GetSection("AppId").Value;
            string metaServer = config.GetSection("apollo").GetSection("MetaServer").Value;
            var  configuration = new ConfigurationBuilder()
                .AddApollo(appId, metaServer)
                // .AddDefault(ConfigFileFormat.Xml)
                // .AddDefault(ConfigFileFormat.Json)
                // .AddDefault(ConfigFileFormat.Yml)
                // .AddDefault(ConfigFileFormat.Yaml)
                .AddDefault().AddNamespace("application")
                .Build();
            _root = configuration;
            return _root;
        }

    注意:如果變量configuration 中沒有讀取到Apollo參數值,可以從configuration 對象的參數中查找返回的異常信息。如果讀取成功會緩存一份文件到本地,如下所示:

      //下面是從Apollo(AppId:100001)的配置中心獲取Key為“ApiKey”的value值:
       string apiKey = GetRoot().GetSection("ApiKey").Value; 

    關於Apollo更多資料,包括Apollo服務端部署,參考官方文檔:https://github.com/ctripcorp/apollo

 

  1.2 Nlog結合Exceptionles

    安裝包如下:

     Install-Package Exceptionless.NLog
      Install-Package NLog.Web.AspNetCore

    在Nlog的基礎上,結合Exceptionles,關鍵代碼如下

      (也可嘗試通過配置文件實現 https://github.com/exceptionless/Exceptionless.Net/tree/master/src/Platforms/Exceptionless.NLog):

     /// <summary>
        /// 返回Nlog.Logger
        /// </summary>
        /// <returns></returns>
        public Logger GetExceptionlessLogger()
        {
            var config = new LoggingConfiguration();
            var exceptionlessTarget = new ExceptionlessTarget();

            //讀取Apploo的Exceptionless配置參數
            string apiKey = ConfigHelper.GetRoot().GetSection("ApiKey").Value;
            string serverUrl = ConfigHelper.GetRoot().GetSection("ServerUrl")?.Value;
            exceptionlessTarget.ApiKey = apiKey;
            exceptionlessTarget.ServerUrl = serverUrl;

            exceptionlessTarget.Layout = "${longdate} |  ${callsite} |  ${level}  | ${message}";
            exceptionlessTarget.Name = "exceptionless";
            //添加exceptionless的Target對象
            config.AddTarget("exceptionless", exceptionlessTarget);
            config.LoggingRules.Add(new LoggingRule("*", global::NLog.LogLevel.Error, exceptionlessTarget));
            LogManager.Configuration = config;
            return LogManager.GetCurrentClassLogger();
     }


        /// <summary>
        /// Nlog.Logger對象
        /// </summary>
        private Logger _logger
        {
            get
            {
                return  N.NLogBuilder.ConfigureNLog("Config\\NLog.config").GetCurrentClassLogger();
            }
        }

        /// <summary>
        /// 帶有Exceptionless的Nlog.Logger對象
        /// </summary>
        private Logger _exceptionlessLogger
        {
            get
            {
                ExceptionlessHelper helper = new ExceptionlessHelper();
                return helper.GetExceptionlessLogger();
            }
        }
     //記錄錯誤日志
      public void Error<T>(IFormatProvider formatProvider, T value)
        {
            _logger.Error(formatProvider, value);
            _exceptionlessLogger.Error(formatProvider, value);
        } 

 

二. Nlog結合Exceptionles代碼優化

  在1.2中介紹了二種日志的結合,下面把1.2的代碼進行優化改造,主要包括:

    (1) 去掉nlog的配置文件,使用編碼來處理

    (2) 記錄日志時,只需要一個_logger.Error,就能同時記錄到txt文件和exceptionless中。

        /// <summary>
        /// Nlog.Logger對象
        /// </summary>
        private Logger _logger
        {
            get
            {
                var config = new LoggingConfiguration();// N.NLogBuilder.ConfigureNLog("Config\\NLog.config");
                //添加exceptionless日志文件
                var exceptionlessTarget = GetExceptionlessTarget();
                config.AddTarget("exceptionless", exceptionlessTarget);
                config.AddRule(LogLevel.Debug, LogLevel.Fatal, exceptionlessTarget);

                //添加txt日志文件
                var logfile = new FileTarget("logfile") { FileName =string.Format(@"c:\temp\{0}.log",DateTime.Now.ToString("yyyy-MM-dd")) };
                logfile.Layout = "${longdate}|${event-properties:item=EventId_Id:whenEmpty=0}|${uppercase:${level}}|${logger}|${message} ${exception:format=tostring}|url: ${aspnet-request-url}|action: ${aspnet-mvc-action}|${callsite}";
                config.AddRule(LogLevel.Debug, LogLevel.Fatal, logfile);

                LogManager.Configuration = config;
                return LogManager.GetCurrentClassLogger();
            }
        }

        /// <summary>
        /// 返回Nlog Target
        /// </summary>
        /// <returns></returns>
        public static ExceptionlessTarget GetExceptionlessTarget()
        {
            var exceptionlessTarget = new ExceptionlessTarget();
            //讀取Apploo配置的Exceptionless地址
            string apiKey = ConfigHelper.GetRoot().GetSection("ApiKey").Value;
            string serverUrl = ConfigHelper.GetRoot().GetSection("ServerUrl")?.Value;
            exceptionlessTarget.ApiKey = apiKey;
            exceptionlessTarget.ServerUrl = serverUrl;
            exceptionlessTarget.Layout = "${longdate} |  ${callsite} |  ${level}  | ${message}";
            exceptionlessTarget.Name = "exceptionless";
            return exceptionlessTarget;
        }

        //記錄日志
        public void Error(string message, Exception exception)
        {
            _logger.Error(exception, message);
        }

  效果如下所示:

    

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM