配置:程序員
1:NuGet程序包 - 搜索log4net - 安裝app
2:配置代碼eclipse
Startup文件ide
#region log4
public static ILoggerRepository repository { get; set; }
#endregion
public Startup(IConfiguration configuration)
{
Configuration = configuration;
#region log4配置
repository = log4net.LogManager.CreateRepository("NETCoreRepository");
log4net.Config.XmlConfigurator.Configure(repository, new FileInfo("log4net.config"));
#endregion
}測試
3:增長配置文件 log4net.configthis
<?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="D:\log4/" /> //存儲路徑
<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>
</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" />
</root>
</log4net>
</configuration>idea
4:編寫代碼debug
(1):調試
public class Log4Controller : Controller
{
private ILog log;
public Log4Controller(IHostingEnvironment hostingEnv)
{
this.log = LogManager.GetLogger(Startup.repository.Name, typeof(Log4Controller));
}
// GET: /<controller>/
public IActionResult Index()
{
log.Error("測試日誌");
return Content("log.Error ok");
}
}日誌
5:日誌級別
trace: 是追蹤,就是程序推動如下,你就能夠寫個trace輸出,因此trace應該會特別多,不過不要緊,咱們能夠設置最低日誌級別不讓他輸出。debug: 調試麼,我通常就只用這個做爲最低級別,trace壓根不用。是在沒辦法就用eclipse或者idea的debug功能就行了麼。info: 輸出一下你感興趣的或者重要的信息,這個用的最多了。warn: 有些信息不是錯誤信息,可是也要給程序員的一些提示,相似於eclipse中代碼的驗證不是有error 和warn(不算錯誤可是也請注意,好比如下depressed的方法)。error: 錯誤信息。用的也比較多。fatal: 級別比較高了。重大錯誤,這種級別你能夠直接中止程序了,是不該該出現的錯誤麼!不用那麼緊張,其實就是一個程度的問題。