在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

相關文章
相關標籤/搜索