基本的に System.Diagnostics.TraceSource クラスから TraceEvent メソッドを呼び出す TraceManager クラスがあります。
TraceSource source = new TraceSource("MyTraceSource");
void TraceInternal(TraceEventType eventType, string message)
{
if (source != null)
{
try
{
source.TraceEvent(eventType, (int)eventType, message);
}
catch (SecurityException)
{
//Cannot access to file listener or cannot have
//privileges to write in event log
//do not propagete this :-(
}
}
}
public void TraceError(string message)
{
if (String.IsNullOrEmpty(message))
throw new ArgumentNullException("message", Messages.exception_InvalidTraceMessage);
TraceInternal(TraceEventType.Error, message);
}
そこで、エラーをトレースするために、TraceError メソッドを呼び出します。
さて、これは WebRole OnStart メソッドで使用するコードです。
DiagnosticMonitorConfiguration dmc = DiagnosticMonitor.GetDefaultInitialConfiguration();
TimeSpan transferPeriod = TimeSpan.FromMinutes(1.0);
dmc.Logs.ScheduledTransferPeriod = transferPeriod;
dmc.Logs.ScheduledTransferLogLevelFilter = LogLevel.Verbose;
DiagnosticMonitor.Start("Microsoft.WindowsAzure.Plugins.Diagnostics.ConnectionString", dmc);
これは私の web.config の構成です。
<system.diagnostics>
<sources>
<source name="MyTraceSource" switchName="sourceSwitch" switchType="System.Diagnostics.SourceSwitch">
<listeners>
<add type="Microsoft.WindowsAzure.Diagnostics.DiagnosticMonitorTraceListener, Microsoft.WindowsAzure.Diagnostics, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" name="AzureDiagnostics">
<filter type="" />
</add>
</listeners>
</source>
</sources>
<switches>
<add name="sourceSwitch" value="Verbose"/>
</switches>
</system.diagnostics>
そして、これらは私の WebRole の構成設定です。
<ConfigurationSettings>
<Setting name="Microsoft.WindowsAzure.Plugins.Diagnostics.ConnectionString" value="DefaultEndpointsProtocol=https;AccountName=ValidAcountName;AccountKey=ValidAcountKey" />
</ConfigurationSettings>
最後に、ServiceDefinition に Diagnostics Import があります。
<Imports>
<Import moduleName="Diagnostics" />
</Imports>
Azure Emulator の下で Visual Studio からアプリケーションを実行すると、正常に動作します。ログをストレージ エミュレーターまたはクラウド ストレージに保存するために ConfigurationSettings を変更することもできます。しかし、Azure にデプロイすると、ログが表示されません。
何か案は?