【问题标题】:Use Event Log (Write in Event Viewer) in Azure在 Azure 中使用事件日志(在事件查看器中写入)
【发布时间】:2016-11-04 21:17:50
【问题描述】:

我的网站是用 ASP.NET 编写的,我使用 EventLog 将日志写入事件查看器。它已经在生产环境中运行(操作系统:Windows Server 2012 R2)并且在记录一些错误时没有遇到任何问题。我现在计划将服务器迁移到 Azure - 应用服务。我想知道在迁移到 Azure - 应用服务后,我记录错误的代码是否仍然有效?如果是,那么我如何查看我的网站记录的错误?我在 Azure - 应用服务中看不到事件查看器。如果不是,那么在记录错误时替换我的代码的最简单和最快的替代方法是什么??

这是我的代码:

public static void LogEventLog(string message, EventLogEntryType logType)
    {
        string source = AppConfig.ErrorEventViewerSource;

        // Since we can't prevent the app from terminating, log this to the event log. 
        CreateEventSource(source);

        // Create an EventLog instance and assign its source.
        EventLog myLog = new EventLog();
        myLog.Source = source;
        myLog.WriteEntry(message, logType);

    }



    public static void CreateEventSource(string source)
    {
        if (!EventLog.SourceExists(source))
        {
            EventLog.CreateEventSource(source, "Application");
        }
    }

【问题讨论】:

标签: c# asp.net azure event-log event-viewer


【解决方案1】:

我认为正确的解决方案是使用here 描述的方法之一将您的应用与应用洞察联系起来。在短期内,为了让我的工作正常,我必须删除对 CreateEventSource() 的调用并写入现有日志,因为我的应用无权在应用服务主机上创建新日志。

【讨论】:

  • 同意我已经切换到应用洞察。我现在将异常错误直接记录到应用程序洞察力中。谢谢!
【解决方案2】:

我知道这是一个老话题,但这可能会帮助像我一样四处搜索的人......

您可以使用Microsoft.Extensions.Logging.ILogger 对象注销。然后在 Azure 中,如果您转到应用服务,左侧菜单,“诊断和解决问题”-> 诊断工具(在主窗格中)-> 左侧菜单上的支持工具/应用程序事件日志新屏幕。

您应该会在该日志流中看到您的事件输出!

我应该注意,这不是完整的详细日志,而是错误。对于完整的日志记录解决方案,请按照之前的建议将您的应用连接到 Application Insights。但是,对于快速简便的解决方案,查看错误输出,这非常方便。

【讨论】:

  • 我不确定您为什么建议在 Azure 应用服务中使用 SharePoint 模块。
  • 这是我的错误,应该是 Microsoft.Extension... 我会更新
  • 不用担心。希望它对未来的人有所帮助。
猜你喜欢
  • 2019-05-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多