ASP.NET Core 3.1: Saving Logs in Application Insights

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
when we deploy an application to production we probably want to use logs to have information about the things that happen in the application then the question arises where are we going to store these logs there are several options one of them is that you create your own provider so that you can do with the log messages what you want for example saved in in your database another option is to use application insights to save these logs application insights is a natural service with which we can monitor our applications it is not limited to being a simple log repository it also helped us to perform performance analysis of our applications and includes analytics to help explore the status of our applications in this video we will focus on the backend of an experiment core application however we can use application insights in other places such as an angular react or nodejs application among others the application we will use is a default application that we obtain when creating an experiment core 3.1 project with a Web API template so let's create the project let's click here SP donut color application we will call it tutorial application insights and we will press create we will choose Web API and we will click on create if you use Visual Studio it is really easy to configure application insights in your project you just have to do the following you have to go to the solution Explorer right click on your project add and choose application insights telemetry and then click on get started and this will pop up a wizard through which you can create your application in such resource and that is also going to configure your application so that it is ready to go though if you are not using Visual Studio or if you want to do this manually you can so let's close this and let's do the process manually for those that are not using Visual Studio the first step is to install a nugget package so let's go here - go get packages browse and let's say Microsoft application insights dot asp.net core analyst is told this except now the second step is to go to the startup class and add the services of application inside so let's say services AB application in size telemetry and here you can choose how to pass what is called the instrumentation key which is an identifier for your application insights resource you can for example use options to specify the configuration of your application inside service or you can for example just simply pass the instrumentation key here or you can leave this blank and this will use a configuration provider to obtain the instrumentation key that is exactly what we are going to do so let's go to the app settings file and notice that I am used in the app settings and not the Absinthe of development the JSON we're going to use that one later and I am going to do the following I am going to say application insights and I am going to create a section which is going to have this property instrumentation key and here I will put my key where does this key come from it comes from an application insights resource so let's go to portal dot a short calm and here we can go to application insights which we have it here and we can create a new one by pressing add you can choose a subscription a resource group I can create a new one let's call it tutorial application insights let's click on OK to tour their application insights and you can choose whatever region is closer to you I will choose East us and review + create and create and now that it has finished let's go to the resource and here to the right we are going to find the instrumentation key we can click on here to copy it and now let's go back to visual studio and let's paste it here and with this we have configure application insights into our application now let's use it in order to save logs in application in session we just have to use the ilogger service that is configured by default in experiment core applications this is achieved by injecting and I lager service into whatever class we want to do the logging so let's go here to the controllers weather forecast controller and we're going to see the world we have an example of how to inject an ilogger service we have a logger and the name of the class in which were in in this case weather forecast controller and then we are saving the instance into a field and here we can put an example here were saying logger debug information warning error and critical these are the different levels that the logger accepts being debug the lowest level and critical the highest level by default application insights is configured to only receive the lock levels warning error and critical we will have to configure it so that we can lock debug and information messages and after that we have an example of an exception here we're saying not implemented reception and here we're saying log over and we're passing the exception and it's message now we can run our application so that these messages get saved into application insights here we can see that the application has rung so we can go here to the port all the data come and let's go down here and under monitoring you are going to see logs and here we can see our logs here you have any screen in which you can write queries for example I can write traces which is the object in which the lock messages are safe in toe and if I press wrong you are going to see that we have no data but that is because sometimes it can take about five minutes for the messages to show up so I will pause the video and we will be right back a few minutes has passed by so now if I click on run again we're going to see some results and as I told you we're only getting warning error and critical messages we're not getting information or debug as I told you also you can write queries here for example I can say pipe order by they say time stamped descending and if I click on run we're going to get our messages order by the time stamp filled in descending order we can also write where classes where message is equal to critical one for example run and as you can see we only get this message so this is a powerful tool to navigate through your logs and of course you can expand here and you can see more information one really useful field is the operation parent ID which allows you to have a correlation ID that identifies all the messages that came from the same HTTP request on your Web API for example let me delete this and let's press run again and you are going to see that they all got the same operation for an ID because they correspond to the same HTTP request you are going to see in a minute that the next batch of messages are going to have the same ID between them but it is going to be different so this ID that we have here now something we're missing here is the exception if you remember here we have an exception a none implementer exception what is it it is not here because it is not a trace but it is an exception so we can double click here and we get exceptions here and we can click on run and here we have the exception the nor implementer exception here we have the time in which it occurred the type of exception be assembly the method and so on we have another place in which we can visualize exceptions which is in the failures section here so let's click on it and here as you can see we have a 404 get favicon.ico which is this icon that we don't have in our project and besides that I have this exception here and I can click on this number and I can click on here and as you can see we get some details of the exception we even have the color stack down here we can click on here just my code and we can even see the line of code in which the what's wrong if we go back we can see that it is indeed on line 40 just like it says here now let's go back here and let's fix our issue with the debug and information messages as I told you the problem is that by default only warning error and critical messages are shown to change this we can go to app settings and we can paste the following code here in the login section as you can see what we're saying is the following instead of logging application insects log level default debug and with this we're saying that we want to show lock messages of the level debug and up which are then debug information warning error and critical so let's change these to two and let's change these to iteration and now let's press ctrl f5 to run our application and as you can see our application has run and now we can go back here we can go to here let's go to logs and again let's click on traces and let's use our order by time stamp in decent order and we have to wait a few minutes before the new batch of messages appear on application insights and now let's click on run and as you can see we're getting critical error warning information and debug which means that our fix actually did the trick and now we are seeing the work and information messages here in the traces object and of course as we said before if we go down here we can see the messages from before and if we go to the column operation parent ID you are going to see that they have different values because they came from different HTTP requests although you can also use the operation ID feel for this the operation parent ID is a shorter one now you probably do not want the logs generated at development time to be displayed on your portal you want only those from your production server to be displayed way to do this is to use different instrumentation keys for development and production for example using app settings let's go to visual studio to see this let's go to the solution Explorer and remember that in the app settings that JSON we have the real instrumentation key so what we want is to copy this and go to the app settings development JSON and here we are going to paste this JSON code but we are going to replace this real instrumentation key by this false instrumentation key it doesn't matter how many zeroes you put here just that whatever value you have here cannot be the real instrumentation key because of the way the app settings files are configured this one is going to substitute the real instrumentation key when you are in development mode and with this you are making sure that when you are in development you are not going to be using your instrumentation key so the log messages are not going to appear on the azure portal finally you may not want to have your key here in your code for security reasons something you can do about it is to put this key in an environment variable in production for example if you have a natural service you can go to the configuration section and configure this as the following you can say you can take this and go to a sure and I will go to and I should have service from my place or course and I will go to configuration and here we can configure application settings we can say new application setting application sighs : instrumentation key instrumentation key and the value is going to be our value that we have here and I can press ok and we can click on save and now we are going to have the instrumentation key available in production and we can delete it from here and what this does is to avoid to have the key in your source code and now you can be sure that unauthorized people are not going to see your key if you liked this video please make sure to subscribe to my channel and let me know what you want me to cover next Thanks
Info
Channel: gavilanch3
Views: 40,690
Rating: undefined out of 5
Keywords:
Id: m7E5itCkCuA
Channel Id: undefined
Length: 13min 28sec (808 seconds)
Published: Fri Jan 31 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.