当前位置: 移动技术网 > IT编程>开发语言>.net > Asp.Net MVC中配置Serilog的方法

Asp.Net MVC中配置Serilog的方法

2017年12月12日  | 移动技术网IT编程  | 我要评论

南宁护士门视频,卓君梦幻狂想曲,九元

一、serilog介绍

serilog 是一种非常简便记录log 的处理方式,使用serilog可以生成本地的text文件, 也可以通过 seq 来在web界面中查看具体的log内容。

二、配置方法

接下来就简单的介绍一下在asp.net mvc中如何配置是serilog 生效:

 1):下载并且安装seq,具体的下载url 为 【http://getseq.net/download】,安装到默认的路径之后,实际上时候启动了一个win service,并且监听的端口号默认为 5341.

安装的最后一步截图如下:

 

然后我们到service列表中可以找到对应的service, 如下图所示:

 

2):创建一个asp.net mvc 5的一个工程, 然后通过 nuget 下载并且安装 对应的 package,如下图所示

  

3):在 app_start 文件夹下创建一个 class 叫做 serilogconfig.cs , 代码如下所示

using serilog;
using serilogweb.classic.enrichers;
using system;
using system.collections.generic;
using system.configuration;
using system.io;
using system.linq;
using system.reflection;
using system.web;
using system.web.hosting;

namespace testserilog.app_start
{
  public class serilogconfig
  {
    public static ilogger createlogger()
    {
      var logpath = hostingenvironment.mappath("~");
      var config = new loggerconfiguration()
        .enrich.withmachinename()
        .enrich.withproperty("applicationname", assemblytitle)
        .enrich.with<httprequestclienthostipenricher>()
        .enrich.with<httprequestrawurlenricher>()
        .enrich.with<httprequestidenricher>()
        .enrich.with<usernameenricher>()
        //.enrich.withproperty("runtimeversion", environment.version)
        // this ensures that calls to logcontext.pushproperty will cause the logger to be enriched
        .enrich.fromlogcontext()
        .minimumlevel.verbose()
        .writeto.seq(configurationmanager.appsettings["seqserver"], apikey: configurationmanager.appsettings["seqapikey"])
        .writeto.rollingfile(path.combine(logpath, "logs\\ericsuntestlog-{date}.log"), retainedfilecountlimit: null, outputtemplate: "{timestamp:yyyy-mm-dd hh:mm:ss.fff zzz} [{level}] {sourcecontext} - ({machinename}|{httprequestid}|{username}) {message}{newline}{exception}");
      return config.createlogger();
    }

    public static string assemblytitle
    {
      get
      {
        var attributes = typeof(serilogconfig).assembly.getcustomattributes(typeof(assemblytitleattribute), false);
        if (attributes.length > 0)
        {
          var titleattribute = (assemblytitleattribute)attributes[0];
          if (titleattribute.title.length > 0)
            return titleattribute.title;
        }
        return path.getfilenamewithoutextension(assembly.getentryassembly().codebase);
      }
    }
  }
}

4):在 web.config 中添加补全所用到的 appsettings

 <appsettings>
  <add key="seqserver" value="http://localhost:5341/" />
  <add key="seqapikey" value="" />
 </appsettings>

5):在 startup.cs 中添加如下代码完成注册

using microsoft.owin;
using owin;
using serilog;
using testserilog.app_start;

[assembly: owinstartupattribute(typeof(testserilog.startup))]
namespace testserilog
{
  public partial class startup
  {
    public void configuration(iappbuilder app)
    {
      configureauth(app);
      log.logger = serilogconfig.createlogger();
    }
  }
}

6): 在 homecontroller 中的 index action 中添加如下代码,测试对应的debuginformationwarning error 方法

using serilog;
using system;
using system.collections.generic;
using system.linq;
using system.web;
using system.web.mvc;

namespace testserilog.controllers
{
  public class homecontroller : controller
  {
    private ilogger _logger = log.logger; 

    public actionresult index()
    {
      _logger.debug("this is index -- debug.");
      _logger.information("this is index -- information.");
      _logger.warning("this is index -- warning.");
      _logger.error("this is index -- error.");
      return view();
    }

    public actionresult about()
    {
      viewbag.message = "your application description page.";

      return view();
    }

    public actionresult contact()
    {
      viewbag.message = "your contact page.";

      return view();
    }
  }
}

7):直接 vs 2015 运行之后, 再去 http://www.lhsxpumps.com/_localhost:5341/#/events 中观察对应的 log 记录, 如下截图

总结

这样简单的配置 serilog 就完成了, 同时我们也可以到 c:\programdata\seq\logs 目录中找到 log 的文本文件。以上就是本文的全部内容了,希望本文的内容对大家的学习或者工作能带来一定的帮助,如果有疑问大家可以留言交流。

更多内容请看如下链接:

http://serilog.net/

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网