.net core 使用nlog

1. 在nuget中安装NLog和NLog.Web.AspNetCore  

2. nlog.config文件的属性

 
3. 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">
  <!-- 要写入的目标 -->
  <targets>
    <!-- 将日志写入文件  -->
    <target xsi:type="File" name="allfile" fileName="${basedir}/logs/all/${level}${shortdate}.log"
            layout="${longdate}|${event-properties:item=EventId_Id:whenEmpty=0}|${uppercase:${level}}|${logger}|${message} ${exception:format=tostring}" />

    <!-- 另一个文件日志,只有自己的日志。 使用一些ASP.NET Core渲染器 -->
    <target xsi:type="File" name="ownFile-web" fileName="${basedir}/logs/my/${level}${shortdate}.log"
            layout="${longdate}
            |${level}
            |信息:${message}
            |url: ${aspnet-request-url}
            " />
  </targets>
  <!--备用配置-->
  <!--<target xsi:type="File" name="ownFile-web" fileName="${basedir}/logs/my/${level}${shortdate}.log"
            layout="${longdate}
            |${level}
            |${logger}
            |信息:${message}
            |url: ${aspnet-request-url}
            |调用函数: ${aspnet-mvc-action}
            |${callsite}
            " />
  </targets>-->

  <!--从记录器名称映射到目标的规则-->
  <rules>
    <!--所有日志,包括来自Microsoft的日志-->
    <logger name="*" minlevel="Trace" writeTo="allfile" />
    <!--跳过非关键的Microsoft日志,因此仅记录自己的日志-->
    <logger name="Microsoft.*" maxlevel="Info" final="true" />
    <!-- BlackHole -->
    <logger name="*" minlevel="Trace" writeTo="ownFile-web" />
  </rules>
</nlog>

4. Program.cs 中添加代码   

  public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
            WebHost.CreateDefaultBuilder(args)
                         .UseUrls("http://*:8010")
          .UseStartup<Startup>()
            .ConfigureLogging(logging =>
                {
                    logging.ClearProviders(); //移除已经注册的其他日志处理程序
                    logging.SetMinimumLevel(LogLevel.Trace); //设置最小的日志级别
                })
                .UseNLog()

            ;

  public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
            WebHost.CreateDefaultBuilder(args)
                         .UseUrls("http://*:8010")
          .UseStartup<Startup>()
            .ConfigureLogging(logging =>
                {
                    logging.ClearProviders(); //移除已经注册的其他日志处理程序
                    logging.SetMinimumLevel(LogLevel.Trace); //设置最小的日志级别
                })
                .UseNLog();


  5. 使用

//引用
using NLog; 

//注意不是 using Microsoft.Extensions.Logging;

//初始化
private readonly ILogger _logger = LogManager.GetLogger("信息");

//调用
_logger.Info($"{DateTime.Now} -服务启动");

 相关连接

日志输出到数据库

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
.Net Core2.1+NLog+数据库连接 <?xml version="1.0" encoding="utf-8" ?> <nlog xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" autoReload="true" throwExceptions="true" internalLogLevel="warn" internalLogFile="logfiles/internal-nlog.txt"> <targets> <target xsi:type="Null" name="blackhole" /> <target name="database" xsi:type="Database" dbProvider="System.Data.SqlClient" connectionString="Data Source=127.0.0.1;Initial Catalog=MiddleData;User ID=lzhu;Password=bl123456;" > <!-- create table NLog ( Id int identity, Application nvarchar(50) null, Logged datetime null, Level nvarchar(50) null, Message nvarchar(512) null, Logger nvarchar(250) null, Callsite nvarchar(512) null, Exception nvarchar(512) null, constraint PK_NLOG primary key (Id) ) --> <commandText> insert into nlog ( Application, Logged, Level, Message, Logger, CallSite, Exception ) values ( @Application, @Logged, @Level, @Message, @Logger, @Callsite, @Exception ); </commandText> <parameter name="@application" layout="NLogTestDemo" /> <parameter name="@logged" layout="${date}" /> <parameter name="@level" layout="${level}" /> <parameter name="@message" layout="${message}" /> <parameter name="@logger" layout="${logger}" /> <parameter name="@callSite" layout="${callsite:filename=true}" /> <parameter name="@exception" layout="${exception:tostring}" /> </target> </targets> <rules> <!--Skip Microsoft logs and so log only own logs--> <logger name="Microsoft.*" minlevel="Trace" writeTo="blackhole" final="true" /> <logger name="NLogTestDemo.*" minlevel="Info" writeTo="database" /> </rules> </nlog>
要在 .NET Core WinForms 应用程序中使用 NLog 日志记录器,需要执行以下步骤: 1. 首先,通过 NuGet 安装 NLog 包。可以使用 Visual Studio 的 NuGet 包管理器或在项目文件中手动添加以下依赖项: ``` <PackageReference Include="NLog" Version="x.x.x" /> ``` 2. 创建 NLog 配置文件。可以在应用程序的根目录中创建一个名为 `nlog.config` 的 XML 文件,并使用以下示例配置: ``` <?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"> <targets> <target name="file" xsi:type="File" fileName="${basedir}/logs/${shortdate}.log" layout="${longdate} ${uppercase:${level}} ${message} ${exception:format=tostring}" /> </targets> <rules> <logger name="*" minlevel="Trace" writeTo="file" /> </rules> </nlog> ``` 该配置将日志记录到位于应用程序根目录下的名为 `logs` 的文件夹中,并将日志级别设置为 `Trace`。 3. 在应用程序代码中引用 NLog 并创建一个日志记录器。可以使用以下代码: ``` using NLog; // ... private static readonly Logger logger = LogManager.GetCurrentClassLogger(); ``` 4. 在需要记录日志的位置调用日志记录器的方法。例如: ``` logger.Debug("Debug message"); logger.Info("Info message"); logger.Warn("Warning message"); logger.Error("Error message"); logger.Fatal("Fatal message"); ``` 这将记录不同级别的日志消息,并在配置文件中指定的位置写入日志文件。 以上步骤应该能够帮助您在 .NET Core WinForms 应用程序中使用 NLog 记录器。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值