我有以下web.config
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" />
<log4net>
<appender name="RollingLogFileAppender" type="log4net.Appender.RollingFileAppender">
<file value="***************\\log.txt" />
<appendToFile value="true" />
<rollingStyle value="Size" />
<!--max size roll backups: number of files produced before overridden (.count) after name-->
<maxSizeRollBackups value="5" />
<maximumFileSize value="10MB" />
<staticLogFileName value="true" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date [%thread] %level %logger - %message%newline" />
</layout>
<filter type="log4net.Filter.LevelRangeFilter">
<levelMin value="WARN" />
<levelMax value="FATAL" />
</filter>
</appender>
<root>
<level value="WARN" />
<appender-ref ref="RollingLogFileAppender" />
</root>
</log4net>
和电子邮件
<system.net>
<mailSettings>
<smtp deliveryMethod="Network" from="**********@***.com">
<network host="smtp.gmail.com" port="***" defaultCredentials="false" userName="**************" password=***********" enableSsl="true" />
</smtp>
</mailSettings>
</system.net>
一切都很好。但是,每次文件翻转时,我都希望通过电子邮件发送日志文件...所以当一个文件已满并且下一个文件开始被附加到我希望通过电子邮件发送完整文件时。我不知道怎么开始这个...
由于
答案 0 :(得分:1)
您必须创建一个继承自RollingFileAppender
的自定义appender,并覆盖AdjustFileBeforeAppend方法以添加邮件文件。以下是滚动您可以修改的RollingFileAppender
文件的现有方法。
使用File
属性查找文件名,并使用System.Net.Mail.SmtpClient
发送邮件。
// log4net.Appender.RollingFileAppender
protected virtual void AdjustFileBeforeAppend()
{
if (this.m_rollDate)
{
DateTime now = this.m_dateTime.Now;
if (now >= this.m_nextCheck)
{
this.m_now = now;
this.m_nextCheck = this.NextCheckDate(this.m_now, this.m_rollPoint);
// send the file here
this.RollOverTime(true);
}
}
if (this.m_rollSize)
{
if (this.File != null && ((CountingQuietTextWriter)base.QuietWriter).Count >= this.m_maxFileSize)
{
// send the file here
this.RollOverSize();
}
}
}