Loggly with AspNetCore and nlog!
First you’ll need a loggly account. If you don’t already have one you can sign up for their free tier here.
Once you’re signed up you’ll need to head over to “Source Setup” > “Customer Tokens” and copy the customer token from the list. We’ll use this token a little bit later.
Next you should create an ASP .NET 2.0 Web API. Note: ensure you have VS 2017 and the relevant NET Core 2.0 SDK installed.
Open Visual Studio
Go to “File” > “New” > “Project” and select “ASP .NET Core Web Application”.
Then select “Web API” and click “ok”.
Next you’ll need to install some nuget packages. Specifically:
<PackageReference Include="NLog" Version="4.5.4" />
<PackageReference Include="NLog.Targets.Loggly" Version="4.7.0" />
<PackageReference Include="NLog.Web.AspNetCore" Version="4.5.3" />
Once those are installed you’ll need to create a new IServiceCollection extension to configure your Loggly library.
Next you’ll need to add an Nlog configuration file. Note: the properties of this file need to be set to “Copy always” or “Copy if newer” in visual studio.
Now you’ll need somewhere to read your loggly customer token from. I strongly advise using the “User Secrets” facility that comes as part of Net Core. This removes the risk of pushing sensitive information to your repository as part of a code commit. That being said you could also store your loggly settings, and all other settings for that matter, in your appsettings.json file. Replace “Your Token” with your loggly customer token.
Now you’ll need to make some changes to your “Startup.cs” file in order to turn on logging and set up your configuration.
Finally you need to inject the ILogger service into your values controller and actually do some logging.
All going well you should be able to run your app, access the values controller and see the log information on your loggly account. Note: it may take a minute or two for loggly to be updated.
Thanks for taking the time to read this and in case you had any problems building the example described above you can always reference the example here.