asp.net core 73 Exceptionless + Nlog series and introduced Apollo

I. Introduction

  The basic use of a Exceptionless in the last chapter, this is mainly about Exceptionless combined Nlog achieve double logging, including Exceptionles the UI visual log and Nlog the txt file log. Then from Apollo reads the configuration file distribution center, when the more massive the system for a long time, parameters to be configured more and more, by using Apollo to unified management center, such as: configuration database connection address, Exceptionless the corresponding item apikey value, Redis connection address, etc. of all the configurable parameters.

 

  1.1 asp.net core in Apollo Configuration

    Open asp.net core project, delete appsettings.json file the default content, add the configuration as follows:

{
  "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 configuration corresponding centers are Apollo client configuration, where the port is 8070 Apollo client interface. Port 8080 is configured to read the address .net core Apollo program. Apollo configuration parameters are stored in the form of key-value.

    Here is the key code reads Apollo configuration file:

      Installation package is as follows:

        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;
        }

    Note: If the variable is not read to the Apollo configuration parameter values, you can find the exception information returned from the parameter configuration object. If the reading is successful cached copy of the file locally, as follows:

      // The following is from Apollo (AppId: 100001) is acquired Key distribution center "ApiKey" value of the value: 
       String the apiKey = GetRoot, () the GetSection (. " ApiKey " ) .Value; 

    For more information about Apollo, including Apollo server deployment, refer to the official document: https://github.com/ctripcorp/apollo

 

  1.2 Nlog combined Exceptionles

    Installation package is as follows:

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

    On the basis of Nlog combination Exceptionles, the key code is as follows

      (You can also try to achieve (through configuration files 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);
        } 

 

Guess you like

Origin www.cnblogs.com/MrHSR/p/11580545.html