Windows Azure 中的持久日志记录
Posted
技术标签:
【中文标题】Windows Azure 中的持久日志记录【英文标题】:Persistent Logging in Windows Azure 【发布时间】:2013-06-07 00:35:41 【问题描述】:到目前为止,我已经尝试了几种不同的策略来将日志保存到我的存储区域。
Trace.Listeners.Add(new TextWriterTraceListener(new Logger(true).output));
Trace.WriteLine("Added a persistant logging listener in Action Worker.");
这是一个记录器:
public class Logger
private string PersistedLogArea = "CustomLoggingLocation";
public StreamWriter output;
public Logger(int hours, int minutes, int ms)
string loglocation = CreatePersistedLogfile();
FileStream fs = new FileStream(loglocation, FileMode.OpenOrCreate, FileAccess.ReadWrite);
output = new StreamWriter(fs);
InitializeWadConfiguration(hours, minutes, ms);
output.Write("If we shadows have offended (persistant logging initialized)");
public Logger(bool interactive)
string loglocation = CreatePersistedLogfile();
FileStream fs = new FileStream(loglocation, FileMode.OpenOrCreate, FileAccess.ReadWrite);
output = new StreamWriter(fs);
InitializeWadConfiguration(0, 1, 0);
output.Write("If we shadows have offended (persistant logging initialized)");
private string CreatePersistedLogfile()
LocalResource localResource =
RoleEnvironment.GetLocalResource("CustomLoggingLocation");
String logPath =
Path.Combine(localResource.RootPath, "Logs");
String fileName =
Path.Combine(logPath, Path.GetRandomFileName());
if (!Directory.Exists(logPath))
Directory.CreateDirectory(logPath);
return fileName;
private void InitializeWadConfiguration(int hours, int minutes, int ms)
//String wadConnectionString =
//"Microsoft.WindowsAzure.Plugins.Diagnostics.ConnectionString";
String customContainerName = "persisted-logfiles";
DiagnosticMonitorConfiguration dmc =
DiagnosticMonitor.GetDefaultInitialConfiguration();
LocalResource localResource =
RoleEnvironment.GetLocalResource(PersistedLogArea);
String logPath =
Path.Combine(localResource.RootPath, "Logs", "logfile.txt");
DirectoryConfiguration directoryConfiguration =
new DirectoryConfiguration()
Container = customContainerName,
DirectoryQuotaInMB =
localResource.MaximumSizeInMegabytes,
Path = logPath
;
dmc.Directories.DataSources.Add(directoryConfiguration);
if (ms > 0)
dmc.Directories.ScheduledTransferPeriod = TimeSpan.FromMilliseconds(ms);
else if (minutes > 0)
dmc.Directories.ScheduledTransferPeriod = TimeSpan.FromMinutes(minutes);
else if (hours > 0)
dmc.Directories.ScheduledTransferPeriod =
TimeSpan.FromHours(hours);
dmc.Logs.BufferQuotaInMB = 100;
dmc.Logs.ScheduledTransferPeriod = TimeSpan.FromHours(1);
dmc.Logs.ScheduledTransferLogLevelFilter =
LogLevel.Verbose;
string connectionString = "DefaultEndpointsProtocol=https;AccountName=mitimesexchangeapp;AccountKey=f7hU2fhpHTgswXShB67MALNWVD2CH4jF+c7Zuw+rDU1U1ZFJoyuPS9BYojAwLxE3oFMEL281i/SYb8YhDbmsSg==";
Microsoft.WindowsAzure.CloudStorageAccount cloudStorageAccount = Microsoft.WindowsAzure.CloudStorageAccount.Parse(connectionString);
DiagnosticMonitor.Start(cloudStorageAccount, dmc);
我运行此代码,但在存储表中什么也看不到。太令人沮丧了。
所以我决定尝试一些基于配置的监听器附件,我做了所有的事情here.
diagnostics.wadcfg:
<?xml version="1.0" encoding="utf-8" ?>
<DiagnosticMonitorConfiguration xmlns="http://schemas.microsoft.com/ServiceHosting/2010/10/DiagnosticsConfiguration"
configurationChangePollInterval="PT1M"
overallQuotaInMB="4096">
<DiagnosticInfrastructureLogs bufferQuotaInMB="0"
scheduledTransferLogLevelFilter="Verbose"
scheduledTransferPeriod="PT30M" />
<Logs bufferQuotaInMB="0"
scheduledTransferLogLevelFilter="Verbose"
scheduledTransferPeriod="PT30M" />
<Directories bufferQuotaInMB="0"
scheduledTransferPeriod="PT30M">
<!-- FailedRequestLogs and IISLogs are only relevant to Web roles -->
<CrashDumps container="wad-crash-dumps" directoryQuotaInMB="0" />
<FailedRequestLogs container="wad-frq" directoryQuotaInMB="0" />
<IISLogs container="wad-iis" directoryQuotaInMB="0" />
<DataSources>
<DirectoryConfiguration container="diagnostics-custom-logs" directoryQuotaInMB="1024">
<LocalResource name="MyCustomLogs" relativePath="."/>
</DirectoryConfiguration>
</DataSources>
</Directories>
<PerformanceCounters bufferQuotaInMB="0" scheduledTransferPeriod="PT30M">
<PerformanceCounterConfiguration counterSpecifier="\Memory\Available Bytes" sampleRate="PT30S" />
<PerformanceCounterConfiguration counterSpecifier="\Processor(_Total)\% Processor Time" sampleRate="PT30S" />
<!-- These three elements are only relevant to Web roles
<PerformanceCounterConfiguration counterSpecifier="\Process(w3wp)\% Processor Time" sampleRate="PT30S" />
<PerformanceCounterConfiguration counterSpecifier="\Process(w3wp)\Private Bytes" sampleRate="PT30S" />
<PerformanceCounterConfiguration counterSpecifier="\Process(w3wp)\Thread Count" sampleRate="PT30S" />-->
<!-- These three elements are only relevant to Worker roles -->
<PerformanceCounterConfiguration counterSpecifier="\Process(WaWorkerHost)\% Processor Time" sampleRate="PT30S" />
<PerformanceCounterConfiguration counterSpecifier="\Process(WaWorkerHost)\Private Bytes" sampleRate="PT30S" />
<PerformanceCounterConfiguration counterSpecifier="\Process(WaWorkerHost)\Thread Count" sampleRate="PT30S" />
<PerformanceCounterConfiguration counterSpecifier="\.NET CLR Interop(_Global_)\# of marshalling" sampleRate="PT30S" />
<PerformanceCounterConfiguration counterSpecifier="\.NET CLR Loading(_Global_)\% Time Loading" sampleRate="PT30S" />
<PerformanceCounterConfiguration counterSpecifier="\.NET CLR LocksAndThreads(_Global_)\Contention Rate / sec" sampleRate="PT30S" />
<PerformanceCounterConfiguration counterSpecifier="\.NET CLR Memory(_Global_)\# Bytes in all Heaps" sampleRate="PT30S" />
<PerformanceCounterConfiguration counterSpecifier="\.NET CLR Networking(_Global_)\Connections Established" sampleRate="PT30S" />
<PerformanceCounterConfiguration counterSpecifier="\.NET CLR Remoting(_Global_)\Remote Calls/sec" sampleRate="PT30S" />
<PerformanceCounterConfiguration counterSpecifier="\.NET CLR Jit(_Global_)\% Time in Jit" sampleRate="PT30S" />
</PerformanceCounters>
<WindowsEventLog bufferQuotaInMB="0"
scheduledTransferLogLevelFilter="Verbose"
scheduledTransferPeriod="PT30M">
<DataSource name="Application!*" />
<DataSource name="System!*" />
</WindowsEventLog>
<Logs bufferQuotaInMB="0"
scheduledTransferLogLevelFilter="Verbose"
scheduledTransferPeriod="PT30M" />
<DiagnosticInfrastructureLogs bufferQuotaInMB="0"
scheduledTransferLogLevelFilter="Verbose"
scheduledTransferPeriod="PT30M" />
<Directories bufferQuotaInMB="0" scheduledTransferPeriod="PT30M">
<DataSources>
<DirectoryConfiguration container="diagnostics-custom-logs" directoryQuotaInMB="128">
<LocalResource name="CustomLogLocation" relativePath="." />
</DirectoryConfiguration>
</DataSources>
</Directories>
</DiagnosticMonitorConfiguration>
配置:
<ConfigurationSettings>
<Setting name="Microsoft.WindowsAzure.Plugins.Diagnostics.ConnectionString" value="DefaultEndpointsProtocol=https;AccountName=<name>;AccountKey=<key>"/>
</ConfigurationSettings>
如果您还有什么想看的,请告诉我。我真的希望我的日志能够保留在我的存储帐户中。
编辑:
它一直在工作。我需要storage explorer 来查看信息,但我不明白它会被放到一张桌子上。阅读理解的胜利。
【问题讨论】:
【参考方案1】:如果要记录跟踪输出,则需要使用 Azure SDK 提供的 TraceListener。
你的 web.config 文件中有这样的东西
<listeners>
<add type="Microsoft.WindowsAzure.Diagnostics.DiagnosticMonitorTraceListener, Microsoft.WindowsAzure.Diagnostics, Version=1.8.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
name="AzureDiagnostics">
<filter type="" />
</add>
</listeners>
或使用代码注册监听器。
一旦配置好所有 Trace.Write... 消息就会以预定义的时间间隔进入WADLogsTable
。而且这不需要任何自定义代码。
希望你看过这个 msdn link
【讨论】:
是的,这不起作用才是重点。它什么也没做。我提供的链接也谈到了这一点,为了简洁起见,我没有粘贴我尝试过的所有内容,也许那是一个错误。 我刚刚注意到您提供了我在帖子中提供的链接,用于我在答案中尝试过的事情。是的,我做了所有这些,但它没有坚持任何东西。 你说得对,我刚刚找到了我的日志在哪里。谢天谢地我昨晚做了这件事,现在我有很多信息。谢谢!以上是关于Windows Azure 中的持久日志记录的主要内容,如果未能解决你的问题,请参考以下文章
根据数据类型增加Azure Log Analytics的数据保留时间
Gnome-Shell-Extension 开发中的持久日志记录?
使用 Windows Azure 上嵌入的 RavenDB 写入日志文件失败