到目前为止,我已经尝试了几种不同的策略来将我的日志保存到我的存储区域。
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才能看到这些信息,我不明白它会去一张桌子。阅读理解胜利。
答案 0 :(得分:1)
如果要记录Trace输出,则需要使用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