Log4Net - 以编程方式创建 Smtpappender

发布于 2024-08-15 01:37:11 字数 1125 浏览 9 评论 0原文

我是 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

離殇 2024-08-22 01:37:11

我没有直接回答你的问题,但有一个有用的建议。
首先使用配置设置对其进行测试,这样您就可以快速消除与代码无关的任何外部问题。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文