我的任务是在 .net 应用程序中记录和跟踪异常。目的是在部署应用程序后记录所有数据。并且信息应该易于追踪以处理错误。我们正在使用 azure 的服务。 为了使问题陈述更清楚,这里有一些演示代码
public bool Connect()
{
try
{
while (!IsConnected())
{
_terminal = new TerminalClient("denaliTermClient", _terminalsrv_host, _terminalsrv_port);
if (_terminal.KillTerminals())
{
_logger.Log("Existing terminals killed successfully");
}
else
{
_logger.Log("Failed killing existing terminals");
}
_connection.Connect(
_terminalsrv_host,
_terminalsrv_port,
_broker,
_account,
_password);
}
return true;
}
catch (Exception ex)
{
long cid = DefaultLogger.CorrelationID;
_logger.Log(cid, ex);
if (ex.Message != null)
{
if (ex.Message.Contains("No connection could be made because the target machine actively refused it"))
{
_logger.Log(cid, "Connection Failed. Please start the Nj4x Terminal Server.");
}
else if (ex.Message.Contains("Invalid user name or password"))
{
_logger.Log(cid, "Nj4x is running in trial mode. Cannot start trading.");
}
}
this.Connect(); // TODO !
}
return false;
}
现在我应该如何实现这个ILogger接口(interface)。我应该使用什么服务? 我没有这方面的经验,所以显然我对此了解不多。希望我已经把问题陈述清楚了。
编辑:我不喜欢登录 .txt 文件的想法。这将包含数千条日志,因此不容易追踪。我正在寻找一些不错的框架,也有助于处理。
最佳答案
如果您正在使用 Azure 服务,我相信 Application Insights 是您的最佳选择。
无需进行大量配置,它就会自动检测未捕获的异常并记录它们以及许多其他内容。您也可以添加自己的痕迹。
Here MSDN 上有一篇很好的文章解释了整个过程。
希望对你有帮助!
关于c# - 如何记录和跟踪 .net 应用程序的异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41738403/