如何在Windows Azure(MVC)中记录错误和用户操作?

时间:2011-11-20 17:03:44

标签: c# azure azure-storage azure-table-storage

Azure变化如此之快,以至于有人可以给我一些关于如何记录的建议:

  • 错误
  • 例外
  • 用户操作

我希望能够将这些记录到表存储中,以便可以使用代码检索它们并在管理网页上查看它们。我对代码看起来并不多,但我真正想知道的是知道我应该在哪里看。 Azure变化如此之快,我希望确保使用最好的。

谢谢

3 个答案:

答案 0 :(得分:5)

Azure内置了功能日志记录和跟踪功能,请参阅

http://msdn.microsoft.com/en-us/magazine/ff714589.aspx

有关该主题的更多信息。

以下是我自己使用Azure诊断的方法:

代码:

using System;
using Microsoft.WindowsAzure.Diagnostics;

namespace CrossCuttingConcerns
{
    /// <summary>
    /// This class handles diagnostics and stores the logs in the Azure table and blog storage.
    /// Note: Basically all logs are turned on here, this can be expensive and you may want to change several settings here before going live
    /// </summary>
    public class AzureDiagnostics
    {
        /// <summary>
        /// Sets how often diagnostics data is transferred to the Azure table storage or blob storage
        /// Note: Change to a period that fits your need, commenting out one of these lines disables it
        /// </summary>
        /// <param name="diagnosticMonitorConfiguration"></param>
        void SetDiagnositcManagerScheduledTransferPeriods(DiagnosticMonitorConfiguration diagnosticMonitorConfiguration)
        {
            diagnosticMonitorConfiguration.Directories.ScheduledTransferPeriod = TimeSpan.FromMinutes(5);
            diagnosticMonitorConfiguration.Logs.ScheduledTransferPeriod = TimeSpan.FromMinutes(5);
            diagnosticMonitorConfiguration.WindowsEventLog.ScheduledTransferPeriod = TimeSpan.FromMinutes(5);
            diagnosticMonitorConfiguration.DiagnosticInfrastructureLogs.ScheduledTransferPeriod = TimeSpan.FromMinutes(5);
            diagnosticMonitorConfiguration.PerformanceCounters.ScheduledTransferPeriod = TimeSpan.FromMinutes(5);
        }

        /// <summary>
        /// Will add a full crashdump. 
        /// Note: Full crashdumps are not available in asp.net roles
        /// </summary>
        void AddFullCrashDumps()
        {
            CrashDumps.EnableCollection(true);
        }

        /// <summary>
        /// Enables performance counters
        /// Note: PerformanceCounterConfiguration.CounterSpecifier is language specific and depends on your OS language.
        /// Note: For a complete list of possible PerformanceCounterConfiguration.CounterSpecifier values run "typeperf.exe /Q"
        /// </summary>
        /// <param name="diagnosticMonitorConfiguration"></param>
        void AddPerformanceCounterMonitoring(DiagnosticMonitorConfiguration diagnosticMonitorConfiguration)
        {
            var performanceCounterConfiguration =
                new PerformanceCounterConfiguration
                {
                    CounterSpecifier = @"\Processor(*)\% Processor Time",
                    SampleRate = TimeSpan.FromSeconds(15)
                };

            diagnosticMonitorConfiguration.PerformanceCounters.DataSources.Add(performanceCounterConfiguration);
        }

        /// <summary>
        /// By default all Windows events to the Application and System logs are stored in the Azure table storage
        /// Note: Decide here what Windows event logs you are interested in seeing, you can also filter out events
        /// </summary>
        /// <param name="diagnosticMonitorConfiguration"></param>
        void AddEventLoggingFromWindowsEventLog(DiagnosticMonitorConfiguration diagnosticMonitorConfiguration)
        {
            // Syntax: <channel>!XPath Query
            // See: http://msdn.microsoft.com/en-us/library/dd996910(VS.85).aspx
            diagnosticMonitorConfiguration.WindowsEventLog.DataSources.Add("Application!*");
            diagnosticMonitorConfiguration.WindowsEventLog.DataSources.Add("System!*");
        }

        void StartDiagnosticManager(DiagnosticMonitorConfiguration diagnosticMonitorConfiguration)
        {
            DiagnosticMonitor.Start("DiagnosticsConnectionString", diagnosticMonitorConfiguration);
        }

        public void EnableAzureDiagnostics()
        {
            var diagnosticMonitorConfiguration = DiagnosticMonitor.GetDefaultInitialConfiguration();

            SetDiagnositcManagerScheduledTransferPeriods(diagnosticMonitorConfiguration);

            AddFullCrashDumps();
            AddPerformanceCounterMonitoring(diagnosticMonitorConfiguration);
            AddEventLoggingFromWindowsEventLog(diagnosticMonitorConfiguration);

            StartDiagnosticManager(diagnosticMonitorConfiguration);
        }
    }
}

配置:

  <system.diagnostics>
    <trace>
      <listeners>
        <add type="Microsoft.WindowsAzure.Diagnostics.DiagnosticMonitorTraceListener, Microsoft.WindowsAzure.Diagnostics, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" name="AzureDiagnostics">
        </add>
      </listeners>
    </trace>
  </system.diagnostics>

答案 1 :(得分:4)

Azure日志记录有了进一步的变化....

通过Trace.TraceXXXX(例如Trace.TraceInformation)进行日志记录现在将记录到Windows Azure文件系统(〜\ LogFiles \ Application * .txt)。

您需要获取对本网站的ftp访问权限(通过Azure管理门户/仪表板/部署凭据启用)才能查看这些文件。

必须首先从网站的“设置”页面启用日志记录,您可以从Visual Studio(服务器资源管理器/ Windows Azure网站/站点名称/视图设置)或Azure管理门户(在“配置/应用程序”下)访问该页面诊断/应用程序记录)。

从Visual Studio输出窗口的实时Windows Web Azure站点也可以看到这些日志(如果右键单击Web,请确保在“显示输出自”下拉列表中选择“Windows Azure Logs - xxx”) Visual Studio Server资源管理器中的站点(在Windows Azure网站下)并选择“在输出窗口中查看流式传输日志”。

Scott Gu博客(http://weblogs.asp.net/scottgu/archive/2013/04/30/announcing-the-release-of-windows-azure-sdk-2-0-for-net.aspx

中介绍了如何登录Visual Studio输出窗口

注意:我在VS2012中只尝试过这个。不确定它是否也适用于VS2010。

答案 2 :(得分:-3)

我周末做了类似的事情。我最后创建了一个名为“LogEvents”的表,并使用提供程序密钥分隔不同类型的日志事件和日期(例如,当有人在11月21日登录时,ProviderKey =“20111121_LoginEvent”。)

对我来说,可以在日期/类型上轻松完成查询,并在管理页面上显示结果

不确定这是否是最佳方式,但似乎对我有用。我确实搜索过Google,但找不到真正做到这一点的任何内容。


更新1: 我使用的类称为LogEvent:

public class LogEntry : TableServiceEntity
{
    public LogEntry(string logType)
    {
        if (LogType == null || LogType.Length == 0)
        {
            if (logType.Length > 0)
                LogType = logType;
            else
                LogType = "Default";
        }

        PartitionKey = string.Format("{0}_{1}", LogType, DateTime.UtcNow.ToString("yyyyMMdd"));
        RowKey = string.Format("{0:10}_{1}", DateTime.MaxValue.Ticks - DateTime.Now.Ticks, Guid.NewGuid());
    }

    public LogEntry()
    {

    }

    public string Message { get; set; }
    public DateTime LogDateTime { get; set; }
    public string LogType { get; set; }

}

我只是创建一个新实例并设置属性:

LogEntry le = new LogEntry("Default") { Message = "Default Page Loaded", LogDateTime=DateTime.Now };
        LogEntryDataSource ds = new LogEntryDataSource();
        ds.AddLogEntry(le);

为了重新获取数据,我只使用标准的Linq查询并传递日期和LogType:

    public IEnumerable<LogEntry> GetLogEntries(string eventType, DateTime logDate)
    {
        var results = from g in this.context.LogEntry
                      where g.PartitionKey == String.Format("{0}_{1}", eventType, logDate.ToString("yyyyMMdd"))
                      select g;
        return results;
    }

可能有更好的方法,但设置非常简单,而且它对我有用

相关问题