Logdashboard 1.1beta. 在 AspNetMvc 中使用日志面板
Logdashboard 是 Net 下的日志面板, 它支持 AspNet 与 AspNetCore 项目. 关于更多 LogDashboard 的介绍请看这里.
就在刚刚 LogDashboard 发布了 1.1 的 beta 版, 在这个版本中有以下变化
https://github.com/liangshiw/LogDashboard/releases
支持 NetFramework 的 AspNetMvc 项目
走势图添加更多趋势
支持 serilog 日志组件
异步查询日志
在 AspNetMvc 中使用日志面板
示例源码 :
使用 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:\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 写一条日志
- 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 的支持与示例
示例源码:
大家可以自行下载体验
异步查询日志
我 fork 了 https://github.com/tmsmith/Dapper-Extensions , 添加了 NetStandard 版本的异步查询支持, 并发布了 Nuget 包
在 LogDashboard 中实现了数据库的异步查询
More
Logdashboard 官网 https://logboard.lpflipped.xin/
LogDashboard 文档 https://logboard.lpflipped.xin/doc/index.html
仓库地址 https://github.com/liangshiw/LogDashboard
欢迎入群交流
来源: https://www.cnblogs.com/LiangSW/p/10310681.html