0

到目前为止,我已经尝试了几种不同的策略来将我的日志保存到我的存储区域。

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

我运行此代码,但在存储表中什么也看不到。太令人沮丧了。

所以我决定尝试一些基于配置的监听器附件,我在这里做了所有的事情。

诊断.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>

让我知道您是否还有其他想看的东西。我真的很希望我的日志保留在我的存储帐户中。

编辑:

它一直在工作。我需要存储资源管理器来查看信息,我不明白它会进入表格。阅读理解为胜利。

4

1 回答 1

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>

或使用代码注册侦听器。

WADLogsTable一旦配置好所有 Trace.Write... 消息将按预定义的时间间隔进入。这不需要任何自定义代码。

希望你看过这个 msdn链接

于 2013-06-07T04:04:32.457 回答