繁体   English   中英

如何将异常记录到 Azure 应用程序日志

[英]How to Log Exceptions to Azure application logs

我有一个 ASP.Net MVC Web 应用程序托管在 Azure Web 服务中。 我需要将异常记录到应用程序日志中。

稍后我想检查并查看发生了什么样的异常。

我该怎么做?

如果您的例外是指那些被困在 try-catch 中的人,那么您可以这样做;

  1. 按照此处提到的步骤,您需要在 Azure web 应用程序上切换此设置;

    您的应用程序 > 应用程序服务日志 > 打开应用程序日志记录(文件系统)

  2. 然后使用下面的代码; Trace.Information(), Trace.Warning(), Trace.Error()

using System.Diagnostics;

public ActionResult sample(){
   try{
      Trace.Information("Hello World!");
   }catch(exception e){
      Trace.Error(e.Message);
   }
}

安装ApplicationInsight nuget package 并在配置文件中添加InstrumentationKey后,您就可以在 Azure 中推送应用程序日志了。

   private readonly ILogger<WeatherForecastController> _logger;

    public WeatherForecastController(ILogger<WeatherForecastController> logger)
    {
        _logger = logger;
    }

    [HttpGet]
    [RequestSizeLimit(40000000)]

    public IEnumerable<WeatherForecast> Get()
    {
        try
        {

        }
        catch (Exception ex)
        {
            _logger.LogError(ex.Message, ex);
        }
    }

您可以在 ApplicationInsights traces和“异常”集合下查看所有日志数据

暂无
暂无

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

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