.NET Core 下使用 NLog 记录日志

NLog is a flexible and free logging platform for various .NET platforms, including .NET standard. NLog makes it easy to write to several targets. (database, file, console) and change the logging configuration on-the-fly.

  • 官网:https://nlog-project.org

  • 开源地址:https://github.com/NLog/NLog

最佳实践

控制台项目

在项目中添加组件包

Install-Package Microsoft.Extensions.Configuration.Json
Install-Package NLog
Install-Package NLog.Extensions.Logging
{
  "Logging": {
    "LogLevel": {
      "Default": "Information",
      "Microsoft": "Warning",
      "Microsoft.Hosting.Lifetime": "Information"
    }
  },
  "AllowedHosts": "*"
}
<?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="internal-nlog.txt">

  <!-- define various log targets -->
  <targets>
    <!-- write logs to file -->
    <target xsi:type="File" name="allfile" fileName="nlog-all-${shortdate}.log"
                 layout="${longdate}|${logger}|${uppercase:${level}}|${message} ${exception}" />


    <target xsi:type="File" name="ownFile-web" fileName="nlog-own-${shortdate}.log"
             layout="${longdate}|${logger}|${uppercase:${level}}|  ${message} ${exception}" />

    <target xsi:type="Null" name="blackhole" />
  </targets>

  <rules>
    <!--All logs, including from Microsoft-->
    <logger name=&#
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,以下是使用NLog日志记录到SqlServer的步骤: 1. 首先,需要在项目中安装NLogNLog.Web.AspNetCore包。可以使用NuGet包管理器或在项目文件中手动添加依赖项。 2. 在项目的appsettings.json文件中添加以下NLog配置: ``` "NLog": { "targets": { "database": { "type": "Database", "dbProvider": "System.Data.SqlClient", "connectionString": "Server=[server];Database=[database];User Id=[user];Password=[password];", "commandText": "INSERT INTO [Logs] ([Date], [Level], [Logger], [Message], [Exception]) VALUES (@Date, @Level, @Logger, @Message, @Exception);", "parameter": [ { "name": "@Date", "layout": "${date}" }, { "name": "@Level", "layout": "${level}" }, { "name": "@Logger", "layout": "${logger}" }, { "name": "@Message", "layout": "${message}" }, { "name": "@Exception", "layout": "${exception}" } ] } }, "rules": [ { "logger": "*", "minLevel": "Trace", "writeTo": "database" } ] } ``` 3. 在Startup.cs文件中添加以下代码: ```csharp public void Configure(IApplicationBuilder app, IWebHostEnvironment env, ILoggerFactory loggerFactory) { //... loggerFactory.AddNLog(); //... app.UseMiddleware<NLogMiddleware>(); //... } ``` 4. 创建一个名为Logs的表,用于存储日志记录。表结构应如下所示: ```sql CREATE TABLE [dbo].[Logs]( [Id] [int] IDENTITY(1,1) NOT NULL, [Date] [datetime2](7) NOT NULL, [Level] [nvarchar](50) NOT NULL, [Logger] [nvarchar](250) NOT NULL, [Message] [nvarchar](max) NOT NULL, [Exception] [nvarchar](max) NULL, CONSTRAINT [PK_Logs] PRIMARY KEY CLUSTERED ( [Id] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] ) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY] ``` 5. 现在,可以在代码中使用NLog记录日志了。例如: ```csharp private readonly ILogger<HomeController> _logger; public HomeController(ILogger<HomeController> logger) { _logger = logger; } public IActionResult Index() { _logger.LogInformation("Hello, world!"); return View(); } ``` 这将在Logs表中插入一条日志记录。 希望这可以帮助到你。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值