log4net configuration file
The introduction of log4net package, create a config directory, designed to put the configuration file, add log4net.config file.
Write configuration file.
<?xml version="1.0" encoding="utf-8"?> <configuration> <log4net> <!-- 错误日志类--> <logger name="errLog"> <level value="ALL" /> <appender-ref ref="ErrorAppender" /> </logger> <!-- 错误日志附加介质--> <appender name="ErrorAppender" type="log4net.Appender.RollingFileAppender"> <->Log file path is automatically created<-! param name = "File" value = "LogError the Log \\ \\" /> <-! whether to append log file -> < param name = "AppendToFile" value = "to true" /> <! - the maximum number of backup log file -> < param name = "MaxSizeRollBackups" value = "2" /> <-! maximum file size (KB / MB / GB) -> < param name = "the MaxFileSize" value = "1MB " /> <! - log file name is to be fixed -> <param name="StaticLogFileName" value= "to false" /> <-! log file name format: 2019-10-09.log -> < param name = "DatePattern" value = "the MM-dd-YYYY & quot; .htm & quot;" /> <! - log rolling basis: mixed mode (file size and date) -> < param name = "RollingStyle" value = "Composite" /> <-! information log layout -> < layout of the type = "log4net.Layout. the PatternLayout " > < param name =" ConversionPattern " value =" & lt; the HR COLOR = Red & gt;% N [Abnormal Time]:% d [% t] & lt; BR & gt;% n [Exception level:% - 5p & lt; BR & gt;% n% m & lt; BR & gt;% n & lt; HR Size = 1 & gt; " /> </layout> </appender> <!-- 信息日志 --> <logger name="infoLog"> <level value="ALL" /> <appender-ref ref="InfoAppender" /> </logger> <!-- 信息日志附加介质--> <appender name="InfoAppender" type="log4net.Appender.RollingFileAppender"> <param name="File" value="Log\\LogInfo\\" /> <param name="AppendToFile" value="true" /> <param name="MaxSizeRollBackups" value="2" /> <param name="MaxFileSize" value="1024KB" /> <param name="StaticLogFileName" value="false" /> <param name="DatePattern" value="yyyy-MM-dd".htm"" /> <param name="RollingStyle" value="Composite" /> <layout type="log4net.Layout.PatternLayout"> <param name="ConversionPattern" value="<HR COLOR=blue>%n【日志时间】:%d [%t] <BR>%n【日志级别】:%-5p <BR>%n%m <BR>%n <HR Size=1>" /> </layout> </appender> </log4net> <!-- 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> </configuration>
Log helper
First, create a storage class
public class Log4NetRepository { public static ILoggerRepository loggerRepository { get; set; } }
/// <Summary> /// log4net helper /// AdoNetAppender only supports the .net framework4.5, .net core does not support the project persisted to the database log /// </ Summary> public class LogHelper { / / abnormality Private static Readonly the ILog LogError = LogManager.GetLogger (Log4NetRepository.loggerRepository.Name, " LogError " ); // record Private static Readonly the ILog the loginfo = LogManager.GetLogger (Log4NetRepository.loggerRepository.Name, " the loginfo " ); public static voidError ( String throwMsg, Exception EX) { String errorMsg = String .Format ( " [Description] Exception: {0} <br> [Type] Exception: {1} <br> [] exception information: {2} <br> call stack []: {}. 3 " , new new Object [] { throwMsg, ex.GetType () the Name,. ex.Message, ex.StackTrace}); errorMsg = errorMsg.Replace ( " \ R & lt \ n- " , " < br> " ); logerror.Error (errorMsg); } Public static void Info ( String Message) { loginfo.Info ( String .Format ( " [] log information: {0} " , Message)); } }
startup reference log4net , log4net.config , log4net.Repository . In such a configuration in the constructor startup:
public static ILoggerRepository repository { get; set; } public Startup(IConfiguration configuration) { Configuration = configuration; // log4net 仓储 repository = LogManager.CreateRepository("CoreLogRepository"); XmlConfigurator.Configure(repository, new FileInfo("config/log4net.config")); Log4NetRepository.loggerRepository = repository; }
Written before the global exception capture, you can now add such a sentence.
public class ApiExceptionFilter:ExceptionFilterAttribute { private IHostingEnvironment _env; public ApiExceptionFilter(IHostingEnvironment env) { _env = env; } public override void OnException(ExceptionContext context) { if (context.ExceptionHandled) { return; } LogHelper.Error(exMsg, context.Exception); // 日志记录 var exMsg = context.Exception.Message; ApiResp resp = new new ApiResp (ApiRespCode.E999999, ExMSG); // development environment display abnormality information IF (_env.IsDevelopment ()) { resp.Message = ExMSG; } context.Result = new new a JsonResult (RESP); context.ExceptionHandled = to true ; } }
When an exception occurs, it will automatically create the Log \ LogError \ directory in the project directory, and written to a log file.