要实现自定义日志输出,您可以按照以下步骤操作:
using log4net;
public class CustomLogger
{
private static readonly ILog log = LogManager.GetLogger(typeof(CustomLogger));
public void LogCustomMessage(string message)
{
log.Info(message);
}
}
<log4net>
<appender name="CustomAppender" type="log4net.Appender.RollingFileAppender">
<param name="File" value="log.txt" />
<param name="AppendToFile" value="true" />
<layout type="log4net.Layout.PatternLayout">
<param name="ConversionPattern" value="%date [%thread] %-5level %logger - %message%newline" />
</layout>
</appender>
<logger name="CustomLogger">
<level value="ALL" />
<appender-ref ref="CustomAppender" />
</logger>
</log4net>
CustomLogger customLogger = new CustomLogger();
customLogger.LogCustomMessage("This is a custom log message");
这样,当您调用LogCustomMessage
方法时,会将日志信息写入到指定的日志文件中。您也可以根据需要自定义日志输出的格式和级别。