标签:control tin append http att manager read set star
一 、配置
1.vs2019 创建一个.net core web程序,选择webapi
2.项目中添加一个配置文件:添加--新建项--XML文件,命名为log4net.config
我使用的是log4net的RollingLogFileAppender,他的好处是按天记录日志,一般日志记录会选择30天
<configuration>
<!-- This section contains the log4net configuration settings -->
<log4net>
<appender name="RollingLogFileAppender" type="log4net.Appender.RollingFileAppender">
<!--文件位置-->
<file value="LogFile/" />
<!--附加文件-->
<appendToFile value="true" />
<!--按天记录-->
<rollingStyle value="Date" />
<datePattern value="yyyyMMdd‘.log‘" />
<staticLogFileName value="false" />
<!--只记录31天-->
<MaxSizeRollBackups value="31" />
<!--输出格式-->
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date [%thread] %-5level %logger [%property{NDC}] - %message%newline" />
</layout>
</appender>
<!-- Setup the root category, add the appenders and set the default level -->
<root>
<level value="ALL" />
<appender-ref ref="RollingLogFileAppender" />
</root>
</log4net>
</configuration>
更多选项参考:http://logging.apache.org/log4net/release/config-examples.html
3.startup配置
public static ILoggerRepository repository { get; set; }
public Startup(IConfiguration configuration)
{
Configuration = configuration;
repository = LogManager.CreateRepository("NETCoreRepository");
XmlConfigurator.Configure(repository, new FileInfo("log4net.config"));
}
二、应用
public class ValuesController : ControllerBase
{
private log4net.ILog log = log4net.LogManager.GetLogger(Startup.repository.Name, typeof(ValuesController));
public ActionResult<string> Get(int id)
{
log.Info($"ValuesController-Get id:{id}");
return "value";
}
}
标签:control tin append http att manager read set star
原文地址:https://www.cnblogs.com/blog-zhaof/p/10655759.html