zoukankan      html  css  js  c++  java
  • ASP.NET Core 添加日志NLog

    1.在Nuget上搜索 NLog.Extensions.Logging 安装最新版

     

    2.添加日志配置文件,在项目指定目录下添加配置文件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"
          internalLogLevel="Warn"
          internalLogFile="c:	empinternal-nlog.txt">
    
      <!-- define various log targets -->
      <targets>
        <!-- write logs to file -->
        <target xsi:type="File" name="allfile" fileName="c:	emp
    log-all-${shortdate}.log"
                     layout="${longdate}|${event-properties:item=EventId.Id}|${logger}|${uppercase:${level}}|${message} ${exception}" />
    
       
        <target xsi:type="File" name="ownFile-web" fileName="c:	emp
    log-own-${shortdate}.log"
                 layout="${longdate}|${event-properties:item=EventId.Id}|${logger}|${uppercase:${level}}|  ${message} ${exception}" />
    
        <target xsi:type="Null" name="blackhole" />
      </targets>
    
      <rules>
        <!--All logs, including from Microsoft-->
        <logger name="*" minlevel="Trace" writeTo="allfile" />
    
        <!--Skip Microsoft logs and so log only own logs-->
        <logger name="Microsoft.*" minlevel="Trace" writeTo="blackhole" final="true" />
        <logger name="*" minlevel="Trace" writeTo="ownFile-web" />
      </rules>
    </nlog>

    3.将nlog.config添加到project.json配置文件中(若配置文件在指定config目录下,可以添加config/*.config)

      "publishOptions": {
            "include": [
                "wwwroot",
                "Views",
                "appsettings.json",
                "web.config",
                "nlog.config"
            ]
        },

    4.在startup.cs文件Configure中添加

    public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
    {
    loggerFactory.AddNLog();
    env.ConfigureNLog("nlog.config");
    …………



    5.记录日志方法
    (1) public class HomeController : Controller
        {
            private static Logger Logger = LogManager.GetCurrentClassLogger();
            public IActionResult Index()
            {
                Logger.Info("Index page says hello");
                return View();
            }
    …………


    (2) public abstract class BaseController<T> : Controller
    {
    protected ILogger<T> Logger { get; set; }
        public BaseController(ILogger<T> logger, IOptions<ApplicationSiteConfig> appConfig)
    {
    this.Logger = logger;
    }
    …………


    参考:https://github.com/NLog/NLog.Extensions.Logging

  • 相关阅读:
    Codeforces Round #652 (Div. 2) A. FashionabLee(几何)
    轻量应用服务器如何通过修改apache配置文件实现非https的访问多域名到不同子目录程序?
    在Windows环境下使用hexo搭建博客以及部署到gitee / github
    使用WordPress搭建个人手机博客(阿里云)
    访问自己服务器的ip地址
    php环境无法上传文件的解决方法
    SSRF漏洞
    CSRF全家桶(含义,防御,攻击)
    JS实现HTML实体与字符的相互转换
    CentOS系统下载及应用部署
  • 原文地址:https://www.cnblogs.com/shouwu/p/6180638.html
Copyright © 2011-2022 走看看