让日志文件不被占用--file value= " logs/ " / !-- 日志生成文件路径 --appendToFi
原文出自https://www.cnblogs.com/AprilBlank/p/11282345.html 非常感谢感动。
1.工具->NuGet承打点器->打点解决方案的NuGet措施包 找到log4net下载
2.在Startup.cs中配置注入
public Startup(IConfiguration configuration) { Configuration = configuration; repository = LogManager.CreateRepository("AprilLog"); XmlConfigurator.Configure(repository, new FileInfo("Config/log4net.config"));//配置文件路径可以自界说 BasicConfigurator.Configure(repository); } //log4net日志 public static ILoggerRepository repository { get; set; } public IConfiguration Configuration { get; }
Startup3.在工程目录下新建一个config文件
代码如下
<?xml version="1.0" encoding="utf-8"?> <configuration> <!-- To customize the asp.net core module uncomment and edit the following section. For more info see https://go.microsoft.com/fwlink/?linkid=838655 --> <!-- <system.webServer> <handlers> <remove name="aspNetCore"/> <add name="aspNetCore" path="*" verb="*" modules="AspNetCoreModule" resourceType="Unspecified"/> </handlers> <aspNetCore processPath="%LAUNCHER_PATH%" arguments="%LAUNCHER_ARGS%" stdoutLogEnabled="false" stdoutLogFile=".\logs\stdout" /> </system.webServer> --> <!-- This section contains the log4net configuration settings --> <log4net debug="false"> <appender name="RollingLogFileAppender" type="log4net.Appender.RollingFileAppender"> <lockingModel type="log4net.Appender.FileAppender+MinimalLock" /><!--很关键的一句,让日志文件不被占用--> <file value="logs/" /> <!-- 日志生成文件路径 --> <appendToFile value="true" /> <rollingStyle value="Composite" /> <staticLogFileName value="false" /> <datePattern value="yyyyMMdd‘.log‘" /> <!-- 日志文件名称格局 --> <maxSizeRollBackups value="10" /> <maximumFileSize value="10MB" /> <!-- 最大文件巨细,到达后生成新文件 --> <layout type="log4net.Layout.PatternLayout"> <conversionPattern value="%date [%thread] %-5level %logger - %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>
log4net.config4.在需要使用的处所创建Log东西 从而使用Info要领记录。
private ILog log = LogManager.GetLogger(Startup.repository.Name, "标题"); log.Info(ex.Message, ex);
使用Log5.会在工程项目中呈现Log文件
亲测可用。
.NET CORE搭建Log日志
,温馨提示: 本文由Jm博客推荐,转载请保留链接: https://www.jmwww.net/file/web/32075.html