Monday, January 9, 2017

Logging in DotNetCore Application

To view any exception raised in the dotnetcore application, these are the steps to do.

Step 1:

First make sure the “web.config” has “stdoutLogEnabled” enabled as “true” and the “stdoutLogFile” path is set correct. Use as shown below. Do not end the path with “\”. The reason is, the application will use the last portion of the name as file prefix along with the timestamp. For example in this case the log file name is something like “stdoutLog_4036_201716232359.log” and this file will be present inside the folder “C:\inetpub\wwwroot\ProjectTest\Logs\”.
    <aspNetCore processPath="%LAUNCHER_PATH%" arguments="%LAUNCHER_ARGS%" stdoutLogEnabled="true" stdoutLogFile="C:\inetpub\wwwroot\ProjectTest\Logs\stdoutLog" forwardWindowsAuthToken="false"/>

Step 2:
Make sure to give write permission to the log folder (in my example ““C:\inetpub\wwwroot\ProjectTest\Logs\”) for the IIS_IUSRS.
 These two steps will ensure logging is happening when an exception is raised in the code.
 I added a “TEST EXCEPTION” in the code like this.
         public void ConfigureServices(IServiceCollection services)       {
            // Add framework services.
            services.AddMvc();
             services.AddTransient<IBizAssign, BizAssign>();
             throw new Exception("TEST EXCEPTION");
        }

This was logged in the log file as per the expectation.
The same log file can be used for non-exception general logging.
Just the follow this article to setup logging. https://docs.microsoft.com/en-us/aspnet/core/fundamentals/logging

Simply start logging wherever necessary like shown below.

            logger.LogInformation("Get function entered.");

This will write all the information in the log file.

All Blogs so far ...