Using TraceSource on Azure and .NET Core with Kudu
When troubleshooting an application production logs are one of the most important diagnostic tools available.
There are many logging libraries and methods that can be used to log errors or tracing. Logs may be stored either on the file system or on a database.
.NET delivers different tools inside the Diagnostic namespace to help the developer troubleshooting and tracing their applications.
This article is an exercise and explores how to use legacy .NET libraries and is not the intended way of doing it.
If you are looking for the intended way of doing it, look at:
Log to file in .NET Core
.NET Core currently does not offer a full file logger out of the box inside their library (see full discussion at https://github.com/aspnet/Logging/issues/441)
I will try to target the .NET Framework, instead than .NET Core, and I will try to log to file using TraceSources.
I implemented my starting project by following this tutorial:
CreateDefaultBuilder
Looking at the initialization boilerplate automatically generated when creating a new .NET Core project with Visual Studio one can notice the method:
WebHost.CreateDefaultBuilder(args)
The source code can be read here
The code will create a WebHostBuilder class that is responsible for building a WebHost that will host the application. This layer is something new in .NET Core the framework cannot rely anymore on the IIS pipeline but will have to handle it on its own.
When configuring the WebHostBuilder, the method configures logging and by default, it includes four different providers:
This is similar to what is explained here.
Target the Full .NET Framework
TraceSources requires to target the full .NET Framework.
“ To use this provider, an app has to run on the .NET Framework (rather than .NET Core).”
That means replacing the “Microsoft.AspNetCore.App” metapackage with all the packages you will need to run your application.
For example, my initial .csproj file was looking like:
<Project Sdk="Microsoft.NET.Sdk.Web"><PropertyGroup>
<TargetFramework>netcoreapp2.1</TargetFramework>
</PropertyGroup><ItemGroup>
<Folder Include="wwwroot\" />
</ItemGroup><ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.App" Version="2.1.2" PrivateAssets="All" />
<PackageReference Include="Microsoft.AspNetCore.Razor.Design" Version="2.1.2" PrivateAssets="All" />
</ItemGroup><ItemGroup>
<Content Update="Index.cshtml">
<CopyToPublishDirectory>PreserveNewest</CopyToPublishDirectory>
<Pack>$(IncludeRazorContentInPack)</Pack>
</Content>
</ItemGroup></Project>
And I turned it into:
<Project Sdk="Microsoft.NET.Sdk.Web"><PropertyGroup><TargetFramework>net472</TargetFramework></PropertyGroup><ItemGroup><Folder Include="wwwroot\" /></ItemGroup><ItemGroup><PackageReference Include="Microsoft.AspNetCore" Version="2.1.2" /><PackageReference Include="Microsoft.AspNetCore.Mvc" Version="2.1.1" /><PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="2.1.1" /><PackageReference Include="Microsoft.EntityFrameworkCore" Version="2.1.1" /><PackageReference Include="Microsoft.Extensions.Logging" Version="2.1.1" /><PackageReference Include="Microsoft.Extensions.Logging.EventSource" Version="2.1.1" /><PackageReference Include="Microsoft.AspNetCore.Mvc.RazorPages" Version="2.1.1" /><PackageReference Include="Microsoft.Extensions.Logging.TraceSource" Version="2.1.1" /><PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="2.1.9" /></ItemGroup></Project>
Configure the TraceSource Provider
In order to configure the trace source, I called the ConfigureLogging method on the WebHostBuilder returned by CreateDefaultBuilder method.
Inside the “AddTraceSource” method I am configuring a TraceSource that will log into a file called at “D:\Home\LogFiles\TraceSources\log.log”. This is the path where I will store my logs on Azure.
It would be interesting to understand how to configure a TraceSource directly from Web.config, however, this will not be the object of this topic.
I also added a logging line inside my Index.cshtml.cs page:
Use Kudu to Read the Log
Kudu is the engine that handles deployments on AppServices. One can deploy using by using FTP, WebDeploy. All other deployments that generally start directly from source control are handled by Kudu.
By connecting to Kudu at the URL:
https://yourwebsite.scm.azurewebsites.net
And looking for the “Debug Console” on the bar on top:
I will create a new folder called “TraceLog”.
Running the application and hitting the “Index.cshtml” page will produce the following result:
Conclusion
It is possible to use TraceSources to log on files in .NET Core and Azure. However, this is not the intended solution to be used.