[英]Error in catch block
I am working on a desktop application where when I thaught of handling the error in catch block in the Event Log then I am getting a error as 我在一个桌面应用程序上工作,当我想处理事件日志中catch块中的错误时,我得到了一个错误
Error 3 The type or namespace name 'EventLog' could not be found (are you missing a using directive or an assembly reference?) 错误3找不到类型或名称空间名称'EventLog'(您是否缺少using指令或程序集引用?)
in this 在这
catch (Exception ex)
{
EventLog log = new EventLog("Application");
log.Source = "MFDBAnalyser";
log.WriteEntry(ex.Message);
}
waiting for suggestions. 等待建议。
Add to the top: 添加到顶部:
using System.Diagnostics;
or use directly: 或直接使用:
catch (Exception ex)
{
System.Diagnostics.EventLog log = new System.Diagnostics.EventLog("Application");
log.Source = "MFDBAnalyser";
log.WriteEntry(ex.Message);
}
or if you're using .NET 3.5 or 4.0 / VS 2008 or 2010 (the most preferred way): 或者,如果您使用的是.NET 3.5或4.0 / VS 2008或2010(最优选的方式):
catch (Exception ex)
{
using (var log = new System.Diagnostics.EventLog("Application") { Source = "MFDBAnalyser" })
{
log.WriteEntry(ex.Message);
}
}
var
reduces the code length. 关键字var
可减少代码长度。 Dispose()
(or use using
block) for classes implementing IDisposable
! 您应该为实现IDisposable
类调用Dispose()
(或使用using
块)! Assuming System.dll
has been referenced by your project (normally the case), you can solve this problem by either of : 假设您的项目已引用System.dll
(通常是这种情况),则可以通过以下任一方法解决此问题:
using System.Diagnostics;
通过添加using System.Diagnostics;
导入名称空间using System.Diagnostics;
directive at the top of your file. 指令位于文件顶部。 System.Diagnostics.EventLog log = ...
完全System.Diagnostics.EventLog log = ...
类型System.Diagnostics.EventLog log = ...
using EventLog = System.Diagnostics.EventLog;
例如,通过using EventLog = System.Diagnostics.EventLog;
进行添加using EventLog = System.Diagnostics.EventLog;
at the top of your file. 在文件的顶部。 have you added a using directive to System.Diagnostics
? 您是否在System.Diagnostics
添加了using指令?
If not, you will have to write it as System.Diagnostics.EventLog
instead. 如果不是,则必须将其编写为System.Diagnostics.EventLog
。
您应该将EventLog
类库引用添加到引用System.Diagnostics
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.