当前位置:网站首页>.net core Web Api中使用log4net日志
.net core Web Api中使用log4net日志
2022-07-13 18:09:00 【故里2130】
1.建立一个.net core Web Api程序,使用nuget下载log4net

2.建立log4net.config文件
<?xml version="1.0" encoding="utf-8"?>
<log4net>
<!-- 控制台日志配置 -->
<appender name="Console" type="log4net.Appender.ConsoleAppender">
<!-- 日志输出格式 -->
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%5level [%thread] (%file:%line) - %message%newline" />
</layout>
</appender>
<!-- 文件存储日志配置 -->
<appender name="RollingFile" type="log4net.Appender.RollingFileAppender">
<!-- 保存文件的名称 -->
<file value="D:\log.log" />
<appendToFile value="true" />
<!-- 文件的编码方式 -->
<param name="Encoding" value="UTF-8"/>
<!-- 每个文件的大小 -->
<maximumFileSize value="100KB" />
<!-- 保存文件数量 -->
<maxSizeRollBackups value="2" />
<!-- 日志输出格式 -->
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%level %thread %logger - %message%newline" />
</layout>
</appender>
<root>
<level value="ALL" />
<appender-ref ref="Console" />
<appender-ref ref="RollingFile" />
</root>
</log4net>3.在Program.cs中进行配置

using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace WebApplication1
{
public class Program
{
public static void Main(string[] args)
{
CreateHostBuilder(args).Build().Run();
}
//进行配置,支持ico
public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
.ConfigureLogging(logging=>
{
logging.AddLog4Net();
})
.ConfigureWebHostDefaults(webBuilder =>
{
webBuilder.UseStartup<Startup>();
});
}
}
4.使用
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace WebApplication1.Controllers
{
[ApiController]
[Route("api/[controller]")]
public class TestController : ControllerBase
{
public TestController(ILogger<TestController> logger )
{
Logger = logger;
}
public ILogger<TestController> Logger = null;
[HttpGet]
public string Get()
{
Logger.LogInformation("1", "3213","123");
Logger.LogError("2", "3213", "1234");
Logger.LogDebug("3", "3213", "1235");
Logger.LogTrace("4", "3213", "1236");
return ".net core-001";
}
}
}
边栏推荐
- jsonp原理
- Redis主从集群搭建及哨兵模式配置
- HCIA复习
- Byte test director stayed up for 10 days, and the test post interview script came out of the liver, giving you wings to your big factory dream~
- Source code compilation and installation redis
- 1、 Disk data recovery experiment report
- STC定时器初值计算
- 3、 Experimental report on the implementation of SMB sharing and FTP construction by freenas
- nodejs使用multe实现文件上传,并将文件存储在服务器
- Day 7 of leetcode question brushing
猜你喜欢

A simple JVM tuning. Write it in your resume

Master-slave copy reading and writing separation nanny level teaching

OSPF实验

Druid database connection pool monitoring page

CCF 201909-1 称检测点查询

It's so delicious. I finally understand why so many people want to switch to software testing~

主从复制读写分离保姆级教学

Basic principle and configuration of switch

RAID disk array

XPath超详细总结
随机推荐
1、 Disk data recovery experiment report
How to write test cases for initial testing? Take you to write a qualified test case from three aspects
GoFrame Step by Step Demo - P1
Rust Thrift Demo
1. STM32F4 USB协议研究 - USB协议
Huawei all connected mGRE and star topology mGRE (full mesh and non full mesh)
C# . Net Core and Net5 Skills record
Rust Thrift Demo
Day 18 of leetcode
3、 Experimental report on the implementation of SMB sharing and FTP construction by freenas
mysql基础相关(重要)
Jsonp principle
CentOS 7x installing MySQL database
Redis master-slave cluster construction and sentinel mode configuration
Idea annotation template, such configuration is enough!
程序猿专属“压测工具”并发模拟
数制转换与子网划分
The difference between set, map, weakset and weakmap
"Finally I left bytek..." confession of a test engineer with an annual salary of 40W
Republish experiment