ExceptionLess ASP.NET MVC 异常日志框架

Exceptionless

      一个开源的实时的日志收集框架,它可以应用在基于 ASP.NET,ASP.NET Core,Web API,Web Forms,WPF,Console,ASP.NET MVC 等技术开发的应用程序中,并且提供了REST接口可以应用在 Javascript,Node.js 中。它将日志收集变得简单易用并且不需要了解太多的相关技术细节及配置。

ExceptionLess官网:http://exceptionless.com/

GitHub:https://github.com/exceptionless/Exceptionless

 

Quick Start

1 官方创建账号

11

 

 

2 创建项目

如果托管在http://exceptionless.com/这个上面,大于一个项目是需要收费的。   

可以自己部署一套源码放到IIS上面  

大家可以参考

 https://www.cnblogs.com/uptothesky/p/5864863.html

 

 

3 ApiKey

11

 

 

 

 

 

 

 

 

 

4 Visual Studio 新建立一个ASP.NET MVC项目

 

5 Web.Config中找到exceptionless节点,apikey属性值设置为官方给你的key

Install-Package Exceptionless.Mvc 成功以后  如下代码会自动加入到web.config
<configSections>
    <section name="exceptionless" type="Exceptionless.ExceptionlessSection, Exceptionless" />   自动加入1
  </configSections>
  <appSettings>
    <add key="webpages:Version" value="3.0.0.0" />
    <add key="webpages:Enabled" value="false" />
    <add key="ClientValidationEnabled" value="true" />
    <add key="UnobtrusiveJavaScriptEnabled" value="true" />
    <add key="ProjectName" value="HelloWord"/>
  </appSettings>
  <exceptionless apiKey="ducXJBe0wLD1yq6IhjGJwyCNnakual1Q7lQFGi1H" /> 自动加入2 将key跟环成你自己的就行

配置完毕  看看效果如何

        /// <summary>
        /// Your application description page.
        /// </summary>
        /// <returns></returns>
        public ActionResult About()
        {
            ViewBag.Message = "Your application description page.";
            throw new Exception("Your application description page.");
        }

F5 运行项目

1

 

然后刷新 https://be.exceptionless.io/dashboard

1

6 集成Logger 日志 统计

创建ExceptionLessLog类

using Exceptionless;
using Exceptionless.Logging;
using Microsoft.VisualBasic.Logging;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace HelloWord.Models
{
    public  class ExceptionLessLog:Log
    {
        /// <summary>
        /// 跟踪
        /// </summary>
        public static void Trace(string message, params string[] tags)
        {
            ExceptionlessClient.Default.CreateLog(message, LogLevel.Trace).AddTags(tags).Submit();
        }

        /// <summary>
        /// 调试
        /// </summary>
        public static void Debug(string message, params string[] tags)
        {
            ExceptionlessClient.Default.CreateLog(message, LogLevel.Debug).AddTags(tags).Submit();
        }

        /// <summary>
        /// 信息
        /// </summary>
        public static void Info(string message, params string[] tags)
        {
            ExceptionlessClient.Default.CreateLog(message, LogLevel.Info).AddTags(tags).Submit();
        }

        /// <summary>
        /// 警告
        /// </summary>
        public static void Warn(string message, params string[] tags)
        {
            ExceptionlessClient.Default.CreateLog(message, LogLevel.Warn).AddTags(tags).Submit();
        }

        /// <summary>
        /// 错误
        /// </summary>
        public static void Error(string message, params string[] tags)
        {
            ExceptionlessClient.Default.CreateLog(message, LogLevel.Error).AddTags(tags).Submit();
        }
    }
}
    /// <summary>
    /// 首页
    /// </summary>
    public class HomeController : Controller
    {

        /// <summary>
        /// 用户登录了111111
        /// </summary>
        /// <returns></returns>
        public ActionResult Index()
        {
            ExceptionLessLog.Info("用户登录了");
            return View();
        }

        /// <summary>
        /// Your application description page.
        /// </summary>
        /// <returns></returns>
        public ActionResult About()
        {
            ViewBag.Message = "Your application description page.";
            ExceptionLessLog.Debug("Your application description page.");
            return View();
        }

        /// <summary>
        /// Your contact page.
        /// </summary>
        /// <param name="name">姓名</param>
        /// <returns></returns>
        public ActionResult Contact(string name)
        {
            ViewBag.Message = "Your contact page.";
            ExceptionLessLog.Error("Your contact page.");
            return View();
        }

    }

2

可以看到一些项目和日志的基本信息,比如Event Type,Level以及标签Tags

1

 

7 ControllerName/actionName 注解

原理:根据swagger一样  根据xml方式获取方法注解

先右键项目 

11

然后在web.config 加入  value="你自己创建的项目名称"

<add key="ProjectName" value="HelloWord"/>

 public class MyActionFilter : ActionFilterAttribute
    {
        /// <summary>
        /// 方法执行后进行的操作
        /// </summary>
        /// <param name="filterContext"></param>
        public override void OnActionExecuted(ActionExecutedContext filterContext)
        {
           
            var con = filterContext.Controller as Controller;

            var controllerName = filterContext.ActionDescriptor.ControllerDescriptor.ControllerName;
            var actionName = filterContext.ActionDescriptor.ActionName;


            var proejctName = ConfigurationManager.AppSettings["ProjectName"] as string;
            //拿到包含注释的xml文档
            var xml = XDocument.Load(con.Server.MapPath(@"~/bin/" + proejctName + ".XML"));
            //拿到方法上的注释
            //拿到方法上的注释没有参数的方法没有(括号
            var summary = (from member in xml.Elements("doc").Elements("members").Elements("member") where member.Attribute("name").Value.ToString().Contains("." + controllerName + "Controller." + actionName + "(") select member.Element("summary").Value).FirstOrDefault() ??
                         (from member in xml.Elements("doc").Elements("members").Elements("member") where member.Attribute("name").Value.ToString().Contains("." + controllerName + "Controller." + actionName) select member.Element("summary").Value).FirstOrDefault();

            string des = $"{controllerName}/{actionName}/{summary}";
            ExceptionlessClient.Default.CreateFeatureUsage(des).AddTags(summary).Submit();

        }
    }

然后添加这个  

    public class FilterConfig
    {
        public static void RegisterGlobalFilters(GlobalFilterCollection filters)
        {
            filters.Add(new HandleErrorAttribute());
            filters.Add(new MyActionFilter());  //添加此处代码
        }
    }

 这样Exceptionless就能很直观看到了

2

posted @ 2018-06-28 14:12  KiSs_小白  阅读(2018)  评论(2编辑  收藏  举报