nlog在.NET Core项目中使用案例

1、新建一个 .NET Core 项目,选择控制台应用程序,名称TestNetCore。

2、使用Nuget程序管理器,添加nlog

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" throwExceptions="false" internalLogLevel="Warn" internalLogFile="logs/nlog.log">
  <targets>
    <!--输出到数据库-->
    <target name="Database" xsi:type="Database" dbProvider="MySql.Data.MySqlClient.MySqlConnection, MySql.Data" connectionString="server=localhost;database=mynetcore;user=root;password=123456;">
      <commandText>
        insert into logs (date,level,message) values(@longdate,@level,@message);
      </commandText>
      <parameter name="@longdate" layout="${longdate}" />
      <parameter name="@level" layout="${level}" />
      <parameter name="@message" layout="${message}" />
    </target>
    <target xsi:type="File" name="File" fileName="Logs/log.txt" layout="${longdate} | ${logger} | ${uppercase:${level}} | ${message} ${exception}" encoding="UTF-8"/>
    <!--输出到普通控制台-->
    <target name="Console" xsi:type="ColoredConsole" layout="${uppercase:${level}}:${longdate} | ${logger} | ${uppercase:${level}} | ${message} ${exception}">
    </target>
  </targets>
  <rules>
    <!--跳过所有Info级别以下的Microsoft组件的日志记录-->
    <logger name="Microsoft.*" minlevel="Info" final="true" />
    <!--注意这里的name="*",如果这里的name=xx了那么在C#的后台.cs文件使用日志的时候必须把xx当做参数来传递,否则没有日志输出-->
    <logger name="*" minlevel="Info" writeTo="Database" />
    <logger name="*" minlevel="Trace" writeTo="Console" />
    <logger name="*" minlevel="Debug" writeTo="File" />
  </rules>
</nlog>

注意:要把nlog.config属性设置成复制到输出目录

4、Program中添加如下

using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;
using System;
using Microsoft.Extensions.Logging;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
using System.Globalization;
using System.Reflection;
using log4net;
using System.IO;
using log4net.Repository;
using log4net.Config;
using NLog;

namespace TestNetCore
{
    internal class Program
    {
        static void Main(string[] args)
        {
            #region  NLog
            Logger logger = NLog.LogManager.GetCurrentClassLogger();

            logger.Fatal("Fatal");
            logger.Error("Error");
            logger.Warn("Warn");
            logger.Info("Info");
            logger.Debug("Debug");
            logger.Trace("Trace");
            #endregion

            Console.ReadLine();
        }
    }
}

5、运行效果

  • 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、付费专栏及课程。

余额充值