gpt4 book ai didi

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

转载 作者:太空狗 更新时间:2023-10-30 00:56:14 29 4
gpt4 key购买 nike

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

  • 错误
  • 异常(exception)
  • 用户操作

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

谢谢

最佳答案

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>

关于c# - 如何在 Windows Azure (MVC) 中记录错误和用户操作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8203108/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com