Dan*_*iel 0 .net c# error-logging windows-services
我在我的代码库中有很多地方,我希望在出现问题时得到通知 - 我想象的很常见.查看以下代码,我如何重新考虑此方法以抽象出电子邮件/日志记录详细信息?目前,这分散在大约100个地方,我真的需要做点什么!
public bool MethodOne() {
string message;
bool success = new Task().Execute();
message = string.Format( "MethodOne was {0} successful.",
success ? "" : "not");
if( !success )
SMTP.send( to, from, message );
System.Diagnostic.EventLog.WriteEntry( executingAssembly, message);
return success;
};
Run Code Online (Sandbox Code Playgroud)
我目前的想法是做类似以下的事情:
public class Log
{
string to = "me@me.com",
from = "system@me.com",
subject = "Error occured";
public void Write( string executingAssembly, string message, bool sendEmail )
{
System.Diagnostic.EventLog.WriteEntry( executingAssembly, message);
if( sendEmail )
SendEmail( to, from, subject, message );
}
private void SendEmail( to, from, subject, message )
{
// Details to send email.
}
}
Run Code Online (Sandbox Code Playgroud)
问题:如何在应用程序中提高日志记录错误和发送通知的可维护性?
考虑使用Logging和Log Manager程序,例如NLog.然后,你可以写一组业务规则在nlog.config登录(或application.config)文件指定不同的位置登录到(目标),你可以指定要记录哪些每个(视病情轻重及类) .
取自NLog网站
一些NLog支持的目标
使用NLog的一些主要特点