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

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

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

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

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

在AspNetMvc中使用日志面板

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

使用VisualStudio建立一个AspNetMvc项目,命名为 NfxAspNetMvc数据库

配置Nlog

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

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

<?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:\temp\nlog-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=\r\n}||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 写一条日志ui

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

安装LogDashboard

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

Install-Package Microsoft.Owin.Host.SystemWeb.net

下面安装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文档

仓库地址

欢迎入群交流

<img src="https://user-images.githubusercontent.com/16813853/51227366-df111580-198e-11e9-9e0c-f7b077e63fe7.png" referrerpolicy="no-referrer">

原文出处:https://www.cnblogs.com/LiangSW/p/10310681.html

相关文章
相关标签/搜索