简体   繁体   English

log4net和邮件

[英]log4net and mailing

I need to modify the existing program that does some error logging by writing to an access file and it actually uses log4net dll (as we found out by checking the dlls via hex editor) so in idea I will need to recompile the tweaked dll with the original that program uses. 我需要修改现有程序,通过写入访问文件来进行一些错误日志记录,并且它实际上使用了log4net dll(正如我们通过十六进制编辑器检查dll所发现的那样),因此在思想上,我需要使用以下命令重新编译经过调整的dll:该程序使用的原始文件。 The need is pretty simple, for some errors that are written to the access file, the system should automaticly warn the admin via sending him an email. 需求非常简单,对于写入访问文件的某些错误,系统应通过向管理员发送电子邮件来自动警告管理员。

What parts of the project and documentation shall I be looking at ? 我应查看项目和文档的哪些部分? I am quite lost as a person who is very new to the .net platform aswell as log4net. 作为.net平台和log4net的新手,我很失落。

EDIT: It seems like the config file of log4net is embedded so no way to access the config directly. 编辑:似乎已嵌入log4net的配置文件,所以没有办法直接访问该配置。 It seems like I will need another way to fix this. 看来我需要另一种方法来解决此问题。 Regards 问候

You probably don't need to change the dlls at all. 您可能根本不需要更改dll。 Your log4net configuration is probably in the App.config file, or a separate configuration file like log4net.xml. 您的log4net配置可能在App.config文件中,或者在单独的配置文件中,例如log4net.xml。

You can set up e-mail notifications for certain types of errors strictly using the configuration file. 您可以严格使用配置文件来为某些类型的错误设置电子邮件通知。

There's a sample on the configuration examples page , just search for SmtpAppender. 配置示例页面上有一个示例,只需搜索SmtpAppender。 If you need to control which messages get sent to e-mail, you can check out the sections of the configuration manual on Loggers and Filters. 如果需要控制将哪些消息发送到电子邮件,则可以查看配置手册中有关“记录器和过滤器”的部分。

If you have the source code of the program, then it is rather simple to change from a hard-coded configuration to configuration file. 如果您拥有程序的源代码,那么从硬编码配置更改为配置文件相当简单。

In case you have no access to the source code, I would use DateTime lastAccessTime = File.GetLastAccessTime(@"c:\\file.txt"); 如果您无法访问源代码,我将使用DateTime lastAccessTime = File.GetLastAccessTime(@"c:\\file.txt"); or DateTime lastWriteTime = File.GetLastWriteTime(@"c:\\file.txt"); DateTime lastWriteTime = File.GetLastWriteTime(@"c:\\file.txt"); to determine the file access with a second program and still use log4net to send the emails. 确定使用第二个程序的文件访问权限,并且仍然使用log4net发送电子邮件。 Trying to tweak the log4net.dll is a lot harder and not recommended from my pov. 尝试调整log4net.dll困难很多,不建议从我的pov中进行调整。

You wrote 你写了

It seems like the config file of log4net is embedded I guess, you have a few options: 我想似乎好像嵌入了log4net的配置文件,您有几种选择:

If you have the source code of the original program, do not embed the Log4Net file and change it according to Don Kirkby suggestions (add a SmtpAppender). 如果您具有原始程序的源代码,请不要嵌入Log4Net文件并根据Don Kirkby的建议进行更改(添加SmtpAppender)。

If you don't have the source code but you are legally alowed to, you can disassemble the program, change the embedded resource (add a SmtpAppender) and put it back into together (eg: csc.exe /out:main.exe /target:exe /resource:main.exe.config main.cs ). 如果您没有源代码,但受到法律的允许,则可以反汇编程序,更改嵌入式资源(添加SmtpAppender)并将其放回原处(例如: csc.exe /out:main.exe /target:exe /resource:main.exe.config main.cs )。

Or, if you don't have the source code and are not allowed to reverse engineer, you can modifiy the way log4net gets configured and load another configuration file instead of the embedded one. 或者,如果您没有源代码且不允许进行反向工程,则可以修改log4net的配置方式,并加载另一个配置文件而不是嵌入式文件。 To do this, I guess this XmlConfigurator Class should be modified. 为此,我猜应该修改XmlConfigurator类 If your original Assembly has been build agains a specific version of log4net you might want to add an assembly redirect so your costumized log4net.dll is used instead. 如果原来的Assembly已重新构建为log4net的特定版本,则可能要添加一个程序集重定向,以便改用您的经过汇总的log4net.dll。 After all you you should configure a SmtpAppender as suggested earlier. 毕竟,您应该按照前面的建议配置SmtpAppender。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM