2011-11-20 54 views
4

Azure的变化如此之快,因此可能有人给我我怎么可能会登录一些建议: 如何在Windows Azure(MVC)中记录错误和用户操作?

  • 例外
  • 用户操作
  • 我希望能够

    • 错误将这些日志记录到表格存储中,以便使用代码检索并在管理网页上查看。我对代码没有太多的了解,但我真正想要的是知道我应该在哪里看。 Azure变化如此之快我想确保使用最好的。

      谢谢

    回答

    -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; 
        } 
    

    有可能是一个更好的办法,但是这是非常简单的设置,它的工作对我来说

    +0

    可以显示表LogEvents的外观吗?我也有兴趣。如何查询日期/类型? –

    +0

    非常感谢格雷格 –

    +0

    我认为如果您在Azure中使用内置的诊断功能而不是自定义诊断功能会更好,请参阅我的回复以获取有关如何执行此操作的更多信息。 – 2011-11-23 14:41:17

    5

    天青已建成功能日志记录和跟踪,看

    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> 
    
    4

    已经有进一步改变Azure的日志记录....

    通过Trace.TraceXXXX(例如Trace.TraceInformation)记录现在将被记录到Windows Az ure文件系统(〜\ LogFiles \ Application * .txt)。

    您需要通过ftp访问网站(通过Azure管理门户/ Dashboard/Deployment Credentials启用)来查看这些文件。

    必须首先从Web站点的Settings页面启用日志记录,您可以从Visual Studio(服务器浏览器/ Windows Azure网站/站点名称/视图设置)或Azure管理门户(在Configure /应用程序诊断/应用程序日志记录)。

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

    登录到Visual Studio Output窗口是覆盖在斯科特谷的博客(http://weblogs.asp.net/scottgu/archive/2013/04/30/announcing-the-release-of-windows-azure-sdk-2-0-for-net.aspx

    注:我只在VS2012尝试这个。不知道它是否也适用于VS2010。