c# - 如何记录和跟踪 .net 应用程序的异常

标签 c# azure logging ms-tracing-eventsource

我的任务是在 .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/

相关文章:

c# - 未指定 asp.net mvc 4 默认路由?

c# - 模数给出错误的结果?

c# - 尝试创建开始最小化的表单时出现 StackOverflowException

azure - 尝试部署 Azure Function 的事件网格订阅时出现 InvalidResourceNamespace 错误

Azure Functions EventHub 触发器缩放作业函数实例

mysql - 我在哪里可以找到 phpMyAdmin 中的 MySQL 日志?

logging - 执行MLT(更像这样)查询时,Elasticsearch中缺少日志记录

c# - AWS DynamoDB - 在对象持久性模型中动态设置 DynamoDBTable

asp.net-mvc-3 - 部署到 azure 时未找到方法 - System.Web.Razor.RazorTemplateEngine.GenerateCode

c# - 如何在多部分/表单数据请求的请求正文中记录文件内容