C# 如何在Windows Azure(MVC)中记录错误和用户操作?
Azure变化如此之快,是否有人能给我一些建议,告诉我如何登录:C# 如何在Windows Azure(MVC)中记录错误和用户操作?,c#,azure,azure-storage,azure-table-storage,C#,Azure,Azure Storage,Azure Table Storage,Azure变化如此之快,是否有人能给我一些建议,告诉我如何登录: 错误 例外情况 用户操作 我希望能够将它们记录到表存储中,以便可以使用代码检索它们并在管理web页面上查看它们。我不是在寻找太多的代码,但我真正想要的是知道我应该在哪里寻找。Azure变化如此之快,我想确保使用最好的 谢谢我在周末做了类似的事情。最后,我只创建了一个名为“LogEvents”的表,并使用provider键来分隔不同类型的日志事件和日期(例如,当有人在11月21日登录时,ProviderKey=“20111121
- 错误
- 例外情况
- 用户操作
谢谢我在周末做了类似的事情。最后,我只创建了一个名为“LogEvents”的表,并使用provider键来分隔不同类型的日志事件和日期(例如,当有人在11月21日登录时,ProviderKey=“20111121_LoginEvent”) 对我来说,查询可以很容易地在日期/类型上完成,并在管理页面上显示结果 不确定这是否是最好的方法,但它似乎对我有效。我在谷歌上搜索过,但找不到任何真正做到这一点的东西
更新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查询并传入日期和日志类型:
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;
}
public IEnumerable GetLogEntries(string eventType,DateTime logDate)
{
var results=来自this.context.LogEntry中的g
其中g.PartitionKey==String.Format(“{0}{1}”),eventType,logDate.ToString(“yyyyMMdd”))
选择g;
返回结果;
}
可能有更好的方法,但安装起来非常简单,而且对我来说很有效Azure内置了日志记录和跟踪功能,请参见 有关此主题的更多信息 以下是我自己使用Azure诊断的方式: 代码:
使用系统;
使用Microsoft.WindowsAzure.Diagnostics;
名称空间横切关注点
{
///
///此类处理诊断并将日志存储在Azure表和日志存储中。
///注意:基本上所有日志都是在这里打开的,这可能会很昂贵,在上线之前,您可能需要在这里更改一些设置
///
公共类AzureDiagnostics
{
///
///设置诊断数据传输到Azure表存储或blob存储的频率
///注意:更改为适合您需要的句点,注释掉其中一行将禁用它
///
///
无效设置DiagnosticManagerScheduledTransferPeriods(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);
}
///
///将添加一个完全崩溃泵。
///注意:完全崩溃转储在asp.net角色中不可用
///
void AddFullCrashDumps()
{
CrashDumps.EnableCollection(true);
}
///
///启用性能计数器
///注意:PerformanceCounterConfiguration.CounterSpecifier是特定于语言的,取决于您的操作系统语言。
///注意:有关可能的PerformanceCounterConfiguration.CounterSpecifier值的完整列表,请运行“typeperf.exe/Q”
///
///
void AddPerformanceCounterMonitoring(诊断监控器配置诊断监控器配置)
{
var性能计数器配置=
新性能计数器配置
{
计数器指定符=@“\Processor(*)\%Processor Time”,
SampleRate=时间跨度从秒(15)
};
diagnosticMonitorConfiguration.PerformanceCounters.DataSources.Add(performanceCounterConfiguration);
}
///
///默认情况下,应用程序和系统日志的所有Windows事件都存储在Azure表存储中
///注意:在这里确定您感兴趣的Windows事件日志,您还可以筛选出事件
///
///
void AddEventLogging fromWindowsEventLog(诊断监视器配置诊断监视器配置)
{
//语法:!XPath查询
//见:http://msdn.microsoft.com/en-us/library/dd996910(第85节)
diagnosticMonitorConfiguration.WindowsEventLog.DataSources.Add(“Application!*”);
diagnosticMonitorConfiguration.WindowsEventLog.DataSources.Add(“System!*”);
}
无效开始诊断管理器(诊断监视器配置诊断监视器配置)
{
DiagnosticMonitor.Start(“DiagnosticConnectionString”,diagnosticMonitorConfiguration);
}
公共无效使能再诊断学()
{
var diagnosticMonitorConfiguration=DiagnosticMonitor.GetDefaultInitialConfiguration();
设置DiagnosticManager计划的传输周期(diagnosticMonitorConfiguration);
AddFullCrashDumps();
添加性能监控(诊断监控配置);
从Windows事件日志(诊断监视器配置)添加事件日志;
StartDiagnosticManager(诊断监视器配置);
}
}
}
配置:
<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>
Azure日志记录有进一步的更改 通过Trace.tracexxx(例如Trace.TraceInformation)进行的日志记录现在将记录到Windows Azure文件系统(~\Lo
<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>