您好,登錄后才能下訂單哦!
這篇文章主要介紹.NET6在WebApi中如何使用日志組件log4net,文中介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們一定要看完!
Microsoft.Extensions.Logging.Log4Net.AspNetCore
<?xml version="1.0" encoding="utf-8" ?> <configuration> <!-- This section contains the log4net configuration settings --> <log4net> <appender name="ConsoleAppender" type="log4net.Appender.ConsoleAppender"> <layout type="log4net.Layout.PatternLayout" value="%date [%thread] %-5level %logger - %message%newline" /> </appender> <appender name="FileAppender" type="log4net.Appender.FileAppender"> <file value="log-file.log" /> <appendToFile value="true" /> <layout type="log4net.Layout.PatternLayout"> <conversionPattern value="%date [%thread] %-5level %logger [%property{NDC}] - %message%newline" /> </layout> </appender> <appender name="RollingLogFileAppender" type="log4net.Appender.RollingFileAppender"> <file value="LogFile/Error/" /> <appendToFile value="true" /> <rollingStyle value="Composite" /> <staticLogFileName value="false" /> <datePattern value="yyyyMMdd'.log'" /> <maxSizeRollBackups value="10" /> <maximumFileSize value="1MB" /> <layout type="log4net.Layout.PatternLayout"> <conversionPattern value="%date [%thread] %-5level %logger [%property{NDC}] - %message%newline" /> </layout> <filter type="log4net.Filter.LevelRangeFilter"> <levelMin value="ERROR" /> <!--配置該節點會監聽的日志錯誤級別--> <levelMax value="FATAL" /> </filter> </appender> <appender name="WarnRollingFileAppender" type="log4net.Appender.RollingFileAppender"> <file value="LogFile/Warn/" /> <appendToFile value="true" /> <rollingStyle value="Date"/> <datePattern value="yyyy-MM-dd'.log'"/> <maxSizeRollBackups value="100" /> <staticLogFileName value="false" /> <encoding value="utf-8" /> <layout type="log4net.Layout.PatternLayout"> <conversionPattern value="%newline%date [%thread %-5level] %n -- %m%n" /> </layout> <filter type="log4net.Filter.LevelRangeFilter"> <levelMin value="WARN" /> <levelMax value="WARN" /> </filter> </appender> <appender name="InfoRollingFileAppender" type="log4net.Appender.RollingFileAppender"> <file value="LogFile/Info/" /> <appendToFile value="true" /> <rollingStyle value="Date"/> <datePattern value="yyyy-MM-dd'.log'"/> <maxSizeRollBackups value="100" /> <staticLogFileName value="false" /> <encoding value="utf-8" /> <layout type="log4net.Layout.PatternLayout"> <!--<conversionPattern value="%newline%date [%thread %-5level] %n %m%n" />--> <conversionPattern value="%date [%thread] %-5level %logger [%property{NDC}] - %message%newline" /> </layout> <filter type="log4net.Filter.LevelRangeFilter"> <levelMin value="TRACE " /> <levelMax value="INFO" /> </filter> </appender> <!-- Setup the root category, add the appenders and set the default level --> <root> <level value="ALL" /> <appender-ref ref="ConsoleAppender" /> <appender-ref ref="FileAppender" /> <appender-ref ref="RollingLogFileAppender" /> <appender-ref ref="WarnRollingFileAppender" /> <appender-ref ref="InfoRollingFileAppender" /> </root> </log4net> </configuration>
Program.cs文件中
ILoggerRepository repository = LogManager.CreateRepository("NETCoreRepository"); XmlConfigurator.Configure(repository, new FileInfo("log4net.config"));
/// <summary> /// 日志 /// </summary> public ILog log = LogManager.GetLogger("NETCoreRepository", this.GetType());
文件生成在項目bin\Debug\net6.0\LogFile\目錄下
以上是“.NET6在WebApi中如何使用日志組件log4net”這篇文章的所有內容,感謝各位的閱讀!希望分享的內容對大家有幫助,更多相關知識,歡迎關注億速云行業資訊頻道!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。