问题
I have web application installed on a VM in Azure (IAAS).
Can I use one of the Azure Diagnostics listeners, such as one of the following
- AzureDriveTraceListener
- AzureTableTraceListener
- AzureBlobTraceListener
or any other listener that will write to Azure Table storage or any other storage in Azure, that I can access using Azure Portal? How I can configure the connection strings?
回答1:
After decompiled the Microsoft.WindowsAzure.WebSites.Diagnostics, I find we couldn't directly setting the connection string by adding some codes.
This package is used for web app service. After enabled the web app Diagnostics logs, the app service will automatic add some environment variables. The Microsoft.WindowsAzure.WebSites.Diagnostics will read these variables to connect to the azure storage and log message to file.
So if you want to use this library to trace error, I suggest you could try to set all the environment variables as web service. But this is too complex.
Here is a workaround, I suggest you could use some log error package to log the error to the azure storage, such as NLog or something else. It contains the extension for azure table storage.
Here are some source code in the Microsoft.WindowsAzure.WebSites.Diagnostics, you could find it read the setting by using EnvironmentVariable:
BaseTraceListener:
protected string GetConfigFile()
{
string environmentVariable = Environment.GetEnvironmentVariable("DIAGNOSTICS_LOGGINGSETTINGSFILE");
string environmentVariable2 = Environment.GetEnvironmentVariable("HOME");
if (environmentVariable == null)
{
return Path.GetFullPath(Path.Combine(environmentVariable2, "site\\diagnostics\\settings.json"));
}
if (Path.IsPathRooted(environmentVariable))
{
return environmentVariable;
}
return Path.GetFullPath(Path.Combine(environmentVariable2, "site", "wwwroot", environmentVariable));
}
AzureblobTrancelistener:
protected override void RefreshConfig()
{
try
{
Config config = base.ReadConfigFile();
base.Enabled = config.AzureBlobEnabled;
base.TraceLevel = config.AzureBlobTraceLevel;
}
catch (Exception innerException)
{
base.Enabled = false;
base.LogException(new ApplicationException(string.Format(Resources.TraceListenerIsDisabledByInvalidConfig, base.GetType().Name), innerException));
}
if (base.Enabled)
{
try
{
string environmentVariable = Environment.GetEnvironmentVariable("DIAGNOSTICS_AZUREBLOBCONTAINERSASURL");
if (string.IsNullOrWhiteSpace(environmentVariable))
{
throw new InvalidOperationException(string.Format(Resources.CloudStorageSasUrlNotSpecified, "DIAGNOSTICS_AZUREBLOBCONTAINERSASURL"));
}
this.blobContainer = new CloudBlobContainer(new Uri(environmentVariable));
}
catch (Exception innerException2)
{
base.Enabled = false;
this.blobContainer = null;
base.LogException(new ApplicationException(string.Format(Resources.TraceListenerIsDisabled, base.GetType().Name), innerException2));
}
}
}
来源:https://stackoverflow.com/questions/43924289/can-i-use-azuretabletracelistener-or-azureblobtracelistener-on-a-vm