Logging to file in asp net core using nlog

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
this is part 63 of asp.net core tutorial in this video well discuss login to a file in asp.net core using third party logging provider n log with a speed on it core we can use the following third party logging providers if we know how to work with one of the third party logging providers working with the others is not that different so let's see how to log to a file using n log the first step is to install the N log new ed package so let's flip over Visual Studio and then within the solution Explorer right-click on the project name and then select manage and you get packages in the window that appears make sure your are on the Browse tab and then search for n log dot web dot asp.net Co NuGet package and then select the first option that we have here and as of this recording the latest stable version is four point eight point two for some reason if you want to install a previous version you can select that from the drop-down list right here I want to install the latest stable version so I'm going to select that and click the install button the video n log NuGet package is successfully installed during the installation you might have noticed a warning on the error list tab complaining that it has detected unreachable code on home controller the reason we're getting this exception is because on line number 33 here we are throwing an exception deliberately and as a result of that the code below this line is unreachable and that's what this warning is complaining about we can safely ignore that warning our new web package and log is successfully installed we are only throwing this exception to demonstrate logging exceptions so after n log negate package is installed we need to configure it for configuration information end log looks for a file with name n log config in the root project directory so I'm going to right click on the project name within solution explorer add we want to add a new item search for a text file I'm going to name this text file and log dot Confed in the interest of time I'm going to paste a minimum configuration that is required with this configuration we're basically telling n log to write the log information to a file that is present in see demo logs folder and the file name is n log - all - today's date will be appended if the file in folder does not exist n log will create them and then we have a logging rule here with this log in rule we are basically enforcing the minimum level of logging that we need we'll discuss this login rule in detail in our next video when we discuss log level setting if you need more information about this configuration information then please refer to n log official documentation on their github page that this URL I'll have this link available both in the description of this video and on my blog at runtime this configuration file n log dot config should be copied to the output directory to enable that right click on and log dot config file select properties in the properties window set this property copy to output directory to copy if nua our next and final step is to add n log as one of the login providers at the moment I am on asp.net core official github page and looking at the code in create default builder method we discussed what this method does in detail in our previous videos in the series one of the things that this method does is it configures logging and we have the code that does that right here notice it's adding console debug and even source has the login providers in addition to these three built-in login providers we also want to add n log so I'm going to copy these five lines of code and paste them in program dot C s file in our project in addition to console debug and even source we also want to add an log as one of the logging providers so on this variable logging I'm going to call add and log extension method I'm going to press control period to bring in the require namespace this extension method is an N log door extensions dot logging namespace with all these changes in place let's run our project in debug mode there we go we have our application up and running and we see the list of all employees when we click on any of the view buttons we have an exception let's continue on the exception notice we are redirected to our custom error view and if we take a look at the debug output window in visual studio notice we have the exception log display right here so in addition to sending this exception log to the debug output vendor destination it should also be logged to a file by n log so if we take a look at C Drive notice within that we have demo logs folder and in this folder we have a file with name and log - all - today's date and if I open this file we have the exception log right here at the moment the size of this file is 3 kilobytes now let's try to navigate to a URL that does not match with any route in our application we are redirected to not found view and this should be logged to the file notice the size is still 3 kilobytes then if we open the file we have of a new log right here at the moment we're only logging messages that have the severity warning and higher in our next video we'll discuss how to control what is logged using log level configuration so 4 simple steps to use n log to log to a file first install n log NuGet package for configure information and log looks for a file with name n log dot config in the root project folder so add a file with this name and include the required configuration that you see here at runtime this configuration file must be copied to the bin folder so our obvious next step is to enable the copy operation finally add an log as one of the logging providers there are several ways to do this we did this in program dot C s file by calling add n log extension method that's it in this video thank you for listening you [Music] you
Info
Channel: kudvenkat
Views: 98,457
Rating: undefined out of 5
Keywords: asp.net core third party logging, asp.net core 2.2 log to file, asp.net core logging to file, asp.net core nlog example, asp.net core add nlog, asp.net core nlog config, asp.net core nlog exception, asp.net core nlog logging, asp.net core mvc nlog, asp.net core nlog setup, asp.net core nlog tutorial, asp.net core log exceptions to file, asp.net core mvc log to file
Id: o5u4fE0t79k
Channel Id: undefined
Length: 7min 29sec (449 seconds)
Published: Thu May 30 2019
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.