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:\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的支持與示例
示例源碼: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
歡迎入群交流
