Is there an alternative to AzureDriveTraceListener to get TraceSource events to appear in Azure web site streaming logs?
Yes, we could use AzureDriveTraceListener to do that. I do a test demo for it.
The following is my detail steps:
1.Create an Asp.net project
2.Reference the Microsoft.WindowsAzure.WebSites.Diagnostics SDK.
3.Add the following config in the web.config file
<system.diagnostics>
<sharedListeners>
<add name="AzureDriveTraceListener" type="Microsoft.WindowsAzure.WebSites.Diagnostics.AzureDriveTraceListener, Microsoft.WindowsAzure.WebSites.Diagnostics, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" />
</sharedListeners>
<sources>
<source name="TraceSourceLogging" switchName="TestSwitch" switchType="System.Diagnostics.SourceSwitch">
<listeners>
<add name="AzureDriveTraceListener"/>
</listeners>
</source>
</sources>
<switches>
<add name="TestSwitch" value="Verbose" />
</switches>
</system.diagnostics>
4.Add an AppTrace class in the project, we could get the demo code from the blog
public static class AppTrace
{
public static TraceSource TraceSource { get; set; }
static AppTrace()
{
TraceSource = new TraceSource("TraceSourceLogging")
{
Switch = {Level = (SourceLevels) Enum.Parse(typeof(SourceLevels), "All", true)}
};
}
private static string Format(string message, string memberName, string filePath, int lineNumber)
{
return $"Message: {message}, MemberName: {memberName}, FilePath: {filePath}, LineNumber: {lineNumber}";
}
public static void Verbose(string message, int id = 16, [CallerMemberName] string memberName = "",
[CallerFilePath] string filePath = "", [CallerLineNumber] int lineNumber = 0)
{
TraceSource.TraceEvent(TraceEventType.Verbose, id, Format(message, memberName, filePath, lineNumber));
}
public static void Error(string message, int id = 2, [CallerMemberName] string memberName = "",
[CallerFilePath] string filePath = "", [CallerLineNumber] int lineNumber = 0)
{
TraceSource.TraceEvent(TraceEventType.Error, id, Format(message, memberName, filePath, lineNumber));
}
public static void Information(string message, int id = 8, [CallerMemberName] string memberName = "",
[CallerFilePath] string filePath = "", [CallerLineNumber] int lineNumber = 0)
{
TraceSource.TraceEvent(TraceEventType.Information, id, Format(message, memberName, filePath, lineNumber));
}
public static void Critical(string message, int id = 1, [CallerMemberName]string memberName =
"", [CallerFilePath] string filePath = "", [CallerLineNumber]int lineNumber = 0)
{
TraceSource.TraceEvent(TraceEventType.Critical, id, Format(message, memberName, filePath, lineNumber));
}
public static void Warning(string message, int id = 4, [CallerMemberName]string memberName =
"", [CallerFilePath] string filePath = "", [CallerLineNumber]int lineNumber = 0)
{
TraceSource.TraceEvent(TraceEventType.Warning, id, Format(message, memberName, filePath, lineNumber));
}
public static void Start(string service, int id = 256, [CallerMemberName]string memberName =
"", [CallerFilePath] string filePath = "", [CallerLineNumber]int lineNumber = 0)
{
TraceSource.TraceEvent(TraceEventType.Start, id, Format("Starting - " + service, memberName, filePath, lineNumber));
}
public static void Stop(string service, int id = 512, [CallerMemberName]string memberName =
"", [CallerFilePath] string filePath = "", [CallerLineNumber]int lineNumber = 0)
{
TraceSource.TraceEvent(TraceEventType.Stop, id, Format("Stoping - " + service, memberName, filePath, lineNumber));
}
}
5.Add a test page.
protected void Page_Load(object sender, EventArgs e)
{
AppTrace.Verbose("Test Verbose");
AppTrace.Error("Test Error");
AppTrace.Warning("Test Warning");
AppTrace.Information("Test Information");
AppTrace.Critical("Test Critical");
Label1.Text = "Completed";
}
6.Publish the project to Azure.
7.Visit the page and check it from Azure portal
