zoukankan      html  css  js  c++  java
  • 在AspNetMvc中使用日志面板. Logdashboard 1.1beta

    Logdashboard 1.1beta. 在AspNetMvc中使用日志面板

    Logdashboard是Net下的日志面板,它支持AspNet与AspNetCore项目。关于更多LogDashboard的介绍请看这里

    就在刚刚LogDashboard发布了1.1的beta版,在这个版本中有以下变化
    https://github.com/liangshiw/LogDashboard/releases

    • 支持NetFramework的AspNetMvc项目
    • 走势图添加更多趋势
    • 支持serilog日志组件
    • 异步查询日志

    在AspNetMvc中使用日志面板

    示例源码 : https://github.com/liangshiw/LogDashboard/tree/master/samples/NfxAspNetMvc

    使用VisualStudio创建一个AspNetMvc项目,命名为 NfxAspNetMvc

    配置Nlog

    在程序包管理控制台安装 Nlog.Web
    Install-Pakcage Nlog.Web

    将下面的Nlog.config添加到项目中,并且配置复制到目录

    <?xml version="1.0" encoding="utf-8" ?>
    <nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd"
          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          autoReload="true"
          throwExceptions="false"
          internalLogLevel="Off" internalLogFile="c:	emp
    log-internal.log">
    
      <!-- optional, add some variables
      https://github.com/nlog/NLog/wiki/Configuration-file#variables
      -->
      <variable name="myvar" value="myvalue"/>
    
      <!--
      See https://github.com/nlog/nlog/wiki/Configuration-file
      for information on customizing logging rules and outputs.
       -->
      <targets>
    
        <target xsi:type="file" name="File" fileName="${basedir}/logs/${shortdate}.log"
                layout="${longdate}||${level}||${logger}||${message}||${exception:format=ToString:innerFormat=ToString:maxInnerExceptionLevel=10:separator=
    }||end" />
        <!--
        Write events to a file with the date in the filename.
        -->
      </targets>
    
      <rules>
        <logger name="*" minlevel="Debug" writeTo="file" />
        <!-- add your logging rules here -->
    
        <!--
        Write all events with minimal level of Debug (So Debug, Info, Warn, Error and Fatal, but not Trace)  to "f"
        <logger name="*" minlevel="Debug" writeTo="f" />
        -->
      </rules>
    </nlog>
    

    打开WebConfig 将下面的modules节点配置复制到 WebConfig

    <modules runAllManagedModulesForAllRequests="true">
          <remove name="TelemetryCorrelationHttpModule" />
          <add name="TelemetryCorrelationHttpModule" type="Microsoft.AspNet.TelemetryCorrelation.TelemetryCorrelationHttpModule, Microsoft.AspNet.TelemetryCorrelation" preCondition="integratedMode,managedHandler" />
          <add name="NLog" type="NLog.Web.NLogHttpModule, NLog.Web" />
        </modules>
    

    打开 HomtController 使用logger 写一条日志

    public ActionResult Index()
    {
        //var logger = logger
        var logger = LogManager.GetCurrentClassLogger();
        logger.Info("HomeController index action");
        return View();
    }
    

    安装LogDashboard

    logDashboard在AspNetMvc中依赖Owin中间件,首先我们先安装 Microsoft.Owin.Host.SystemWeb

    Install-Package Microsoft.Owin.Host.SystemWeb

    下面安装LogDashboard 因为是预发布版,在安装的时候需要指定版本

    Install-Package LogDashboard -Version 1.1.0-beta

    最后我们添加Owin Startup类,在添加时搜索owin会出现该条目,在Startup中配置LogDashboard中间件

    public void Configuration(IAppBuilder app)
    {
        app.MapLogDashboard(typeof(Startup).Assembly, opt =>
        {
            opt.SetRootPath(AppContext.BaseDirectory);
        });
    }
    

    这时运行项目并导航到 /logdashboard 就会看到日志面板,与我们添加的日志消息 HomeController index action

    走势图

    在1.1中添加了以小时、天、周、月为单位的日志趋势图

    支持serilog

    除了log4net、Nlog之外 LogDashboard还加入了serilog的支持与示例

    示例源码:https://github.com/liangshiw/LogDashboard/tree/master/samples/UseSerilog
    大家可以自行下载体验

    异步查询日志

    我fork了 https://github.com/tmsmith/Dapper-Extensions ,添加了NetStandard版本的异步查询支持,并发布了Nuget包 https://www.nuget.org/packages/DapperExtensions.Standard/
    在LogDashboard中实现了数据库的异步查询

    More

    Logdashboard官网

    LogDashboard文档

    仓库地址

    欢迎入群交流

  • 相关阅读:
    strcpy和memcpy的区别(转)
    获得discuz7.2 目录下所有文件,并写到txt
    Lintcode: Majority Number 解题报告
    Lintcode: Fast Power 解题报告
    LeetCode: Pow(x, n) 解题报告
    Lintcode: Minimum Subarray 解题报告
    Lintcode: Subarray Sum 解题报告
    LeetCode: Minimum Depth of Binary Tree 解题报告
    LeetCode: Binary Tree Maximum Path Sum 解题报告
    LeetCode: Binary Tree Postorder Traversal 解题报告
  • 原文地址:https://www.cnblogs.com/LiangSW/p/10310681.html
Copyright © 2011-2022 走看看