要實現自定義日志輸出,您可以按照以下步驟操作:
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
方法時,會將日志信息寫入到指定的日志文件中。您也可以根據需要自定義日志輸出的格式和級別。