Log4Net - 以编程方式创建 Smtpappender
我是 log4net 的初学者,我正在尝试在代码中添加附加程序,然后在出现错误时发送电子邮件。我不想使用配置文件。我的程序运行了,但我从未收到电子邮件。我的代码有什么问题?
Dim test2 As log4net.Appender.SmtpAppender = New log4net.Appender.SmtpAppender()
test2.To = [email protected]
test2.From = [email protected]
test2.Subject = "test Email report"
test2.SmtpHost = "localhost"
test2.BufferSize = 1
test2.Lossy = true
Dim layout As log4net.Layout.PatternLayout = New log4net.Layout.PatternLayout()
layout.ConversionPattern = "%newline%message"
test2.Layout = layout
test2.ActivateOptions()
Dim l As log4net.Repository.Hierarchy.Logger = log4net.LogManager.GetLogger("EmailLog").Logger
l.Level = log4net.Core.Level.Error
l.Additivity = True
l.Repository.Configured = True
l.AddAppender(test2)
msg = "test test test" & vbNewLine & vbNewLine
Dim logger As log4net.ILog = log4net.LogManager.GetLogger("EmailLog")
logger.Error(msg)
I am a beginner in log4net and I am trying to add an appender in my code and then to send an email in case of an Error. I don't want to use a config file. My program runs but I never receive the email. What's wrong in my code?
Dim test2 As log4net.Appender.SmtpAppender = New log4net.Appender.SmtpAppender()
test2.To = [email protected]
test2.From = [email protected]
test2.Subject = "test Email report"
test2.SmtpHost = "localhost"
test2.BufferSize = 1
test2.Lossy = true
Dim layout As log4net.Layout.PatternLayout = New log4net.Layout.PatternLayout()
layout.ConversionPattern = "%newline%message"
test2.Layout = layout
test2.ActivateOptions()
Dim l As log4net.Repository.Hierarchy.Logger = log4net.LogManager.GetLogger("EmailLog").Logger
l.Level = log4net.Core.Level.Error
l.Additivity = True
l.Repository.Configured = True
l.AddAppender(test2)
msg = "test test test" & vbNewLine & vbNewLine
Dim logger As log4net.ILog = log4net.LogManager.GetLogger("EmailLog")
logger.Error(msg)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我没有直接回答你的问题,但有一个有用的建议。
首先使用配置设置对其进行测试,这样您就可以快速消除与代码无关的任何外部问题。
I don't have a direct answer to your question, but a useful suggestion.
Test it using the configuration setup first, that way you can quickly eliminate any external problems unrelated to your code.