设为首页 加入收藏

TOP

asp.net core系列 73 Exceptionless+Nlog以及Apollo介绍(一)
2019-09-25 11:18:20 】 浏览:94
Tags:asp.net core 系列 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.L
首页 上一页 1 2 下一页 尾页 1/2/2
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇.NET MVC5简介(五)管道处理模型.. 下一篇两数之和

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目