良好的示例.Net Windows 服务报告错误

发布于 2024-09-10 05:28:54 字数 294 浏览 2 评论 0原文

我正在编写一个 Windows 服务,它将进行大量的网络通信(在共享文件夹中复制许多文件并修改数据库)。

我需要一种方法来通知用户(如果已登录)任何异常/错误。我的问题是对错误进行分组以将其(通过电子邮件)发送到管理员地址。

我知道事件记录既没有从系统托盘中弹出通知,但管理员不会查看该日志,他更喜欢电子邮件。

理想的组件是 ASP.NET 运行状况监控,但仅适用于 IIS,并且需要与 Windows 服务类似的组件。

关于此问题的任何 C# 或其他语言(带有源代码)中的示例应用程序 Windows 服务?

I am writing a windows service that will be doing a lot of network communication (copy many many files in shared folders and modify database).

I need a way to notify a user (if logged on) of any exceptions/errors. My issue is do group to errors for send it (by email) to administrator address.

I am aware of event logging neither notification bubble from system tray, but the administrator not views that log, he prefers email.

The ideal component is ASP.NET Health Monitoring, but only works for IIS, and it is required a component similar for Windows Services.

Any sample application Windows Service in C# or another language in .net (with source code) about this issue ??

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

坚持沉默 2024-09-17 05:28:54

如果您只需要一种发送电子邮件通知的方法,.Net 具有 SMTP 和邮件类型来处理此问题。电子邮件通知在软件中不再常见,这就是常用功能已合并到基类库中的原因。

您可以使用 SmtpClient、MailAddress 和 MailMessage 对象来完成发送电子邮件通知所需的任务。当然,您需要访问 SMTP 服务器才能传输邮件,因此请找出其主机地址以正确配置您的应用程序。下面是一些示例:

SmtpClient  mailClient = new SmtpClient("smtp.fu.bar");
MailAddress senderAddr = new MailAddress("[email protected]");
MailAddress  recipAddr = new MailAddress("[email protected]");
MailMessage   emailMsg = new MailMessage( senderAddr, recipAddr );

emailMsg.Subject = "Test email.";
emailMsg.Body = "Here is my email string which serves as the body.\n\nSincerely,\nMe";
mailClient.Send( emailMsg );

该示例只是直接代码,但最好将其放入可重用的方法中,如下所示:

public void SendNotification( string smtpHost, string recipientAddress, string senderAddress, string message, string subject )
{
    SmtpClient  mailClient = new SmtpClient(smtpHost);
    MailMessage   emailMsg = new MailMessage( new MailAddress(senderAddress), new MailAddress(recipientAddress) );

    emailMsg.Subject = subject;
    emailMsg.Body = message;

    mailClient.Send( emailMsg );
}

If you're just needing a way to send an email notification, .Net has SMTP and mail types to take care of this. Email notifications are common in software anymore and that's why the commonly used functionality has been incorporated into the base class lib.

You could use SmtpClient, MailAddress, and MailMessage objects to accomplish what you need to simply send an email notification. Of course you need to have access to an SMTP server to transmit the mail, so find out what its host address is to properly configure your application. Here are a few examples:

SmtpClient  mailClient = new SmtpClient("smtp.fu.bar");
MailAddress senderAddr = new MailAddress("[email protected]");
MailAddress  recipAddr = new MailAddress("[email protected]");
MailMessage   emailMsg = new MailMessage( senderAddr, recipAddr );

emailMsg.Subject = "Test email.";
emailMsg.Body = "Here is my email string which serves as the body.\n\nSincerely,\nMe";
mailClient.Send( emailMsg );

That example is just straight code, but it would be better to put it into a reusable method like this:

public void SendNotification( string smtpHost, string recipientAddress, string senderAddress, string message, string subject )
{
    SmtpClient  mailClient = new SmtpClient(smtpHost);
    MailMessage   emailMsg = new MailMessage( new MailAddress(senderAddress), new MailAddress(recipientAddress) );

    emailMsg.Subject = subject;
    emailMsg.Body = message;

    mailClient.Send( emailMsg );
}
反话 2024-09-17 05:28:54

检查这个:http://www. codeproject.com/Articles/16335/Simple-Windows-Service-which-sends-auto-Email-aler

如果这是第一次使用 Windows 服务(因为 Windows 服务很难调试),我的建议

  • 首先 使用 C# 进行命令行应用程序将其移至 Windows 服务
  • 当您确定项目的所有功能时,

check this : http://www.codeproject.com/Articles/16335/Simple-Windows-Service-which-sends-auto-Email-aler

my advice if it's first time to work with windows service (because Windows Service is very hard to debug)

  • first make it Command Line application with c#
  • when you be certain with all functionality of your project move it to Windows Service
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文