You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: lessons/dotnet8/logging/README.md
+57-22Lines changed: 57 additions & 22 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -25,10 +25,10 @@ This lesson consists of the following exercises:
25
25
26
26
| Prerequisite | Exercise
27
27
| - | -
28
-
| Azure Functions Core Tools | 1-6
29
-
| VS Code with Azure Functions extension| 1-6
30
-
| REST Client for VS Code or Postman | 1-6
31
-
| Azure Subscription | 2-7
28
+
| Azure Functions Core Tools | 1-5
29
+
| VS Code with Azure Functions extension| 1-5
30
+
| REST Client for VS Code or Postman | 1-5
31
+
| Azure Subscription | 2-5
32
32
33
33
See [.NET 8 prerequisites](../prerequisites/README.md) for more details.
34
34
@@ -77,32 +77,53 @@ This exercise is a condensed version of the
77
77
78
78
You may have noticed that some lines have been printed to the Console each time the HTTP function is triggered. Unfortunately, the Console log output is very limited and does not offer much details on the structure of logs.
79
79
80
-
Most Azure Functions end up logging to Azure Monitor. In particular, _Application Insights_ is a comprehensive Application Performance Monitoring component of Azure Monitor that, amgonst other things, collects telemetry from a running application. Logs − or traces − are one of many signals of telemetry that _Application Insights_ collects.
80
+
Most Azure Functions end up logging to Azure Monitor. In particular, _Application Insights_ is a comprehensive Application Performance Monitoring component of Azure Monitor that, amongst other things, collects telemetry from a running application. Logs − or traces − are one of many signals of telemetry that _Application Insights_ collects.
81
81
82
-
In this section, you will learn the basics of _Application Insights_ and its _Live Metrics_ dashboard.
82
+
### Overview
83
+
84
+
Before diving into the code for this exercise, it is critical to understand how your Function App works as it directly drives the configuration for logging to _Application Insights_ successfully.
85
+
86
+
Azure Functions is a compute infrastructure that runs your _functions-as-a-service_ workload
87
+
through the _Functions Runtime Host_ process.
88
+
89
+
The Function App itself is a .NET Console application that runs as a separate process referred to as the _Worker_ process.
90
+
By linking to the `Microsoft.Azure.Functions.Worker` NuGet package, the program starts a remote-process server and waits for the
91
+
communication from the host using the [gRPC](https://grpc.io/) protocol.
92
+
93
+

94
+
95
+
You may already be familiar with the `host.json` file that is used to configure the host.
96
+
97
+
By default, a Function App running as an isolated worker process does not use a separate configuration
98
+
file and relies entirely on environment variables and application settings in Azure. As you will see later
99
+
in this lesson, however, it is often desirable to use a separate file for the log configuration.
100
+
101
+
As your isolated worker process runs – as its name suggests – in a separate process from the host,
102
+
it needs its own file to store its own settings and configurations for logging. In this lesson, the
103
+
program will load the log (a subset of) the configuration from the `host.json` file,
104
+
making it a shared file for both the host and the worker process itself.
105
+
106
+
In the following section, you will learn the basics of _Application Insights_ and its _Live Metrics_ dashboard.
83
107
84
108
### Steps
85
109
86
110
1. Navigate to the Azure Portal and create [a new resource group](https://portal.azure.com/#view/Microsoft_Azure_Marketplace/GalleryItemDetailsBladeNopdl/id/Microsoft.ResourceGroup) named `AzFuncUni`, for instance.
87
111
88
112
2. Navigate to the newly created resource group and create [a new _Application Insights_](https://portal.azure.com/#view/Microsoft_Azure_Marketplace/GalleryItemDetailsBladeNopdl/id/Microsoft.AppInsights) resource. This may also create a _Log Analytics Workspace_ resource.
89
113
90
-
3. Go to the newly created resource and notice the `Essentials` section, at the top of the center pane. Please take note of the `Instrumentation Key` and `Connection String` properties.
114
+
3. Go to the newly created resource and notice the `Essentials` section, at the top of the center pane. Please take note of the and `Connection String` property.
91
115
92
116
4. Back to your local working folder, open the `local.settings.json` project file and add two corresponding properties in the `Values` section:
> 📝 **Tip** - The `APPINSIGHTS_INSTRUMENTATIONKEY` property is being deprecated. However, at the time of writing, it is still necessary to enable _Live Metrics_ on the Azure Portal when the application is run locally.
105
-
106
127
5. Open the `host.json` file and replace its content with:
107
128
108
129
```json
@@ -111,7 +132,7 @@ In this section, you will learn the basics of _Application Insights_ and its _Li
111
132
"logging": {
112
133
"applicationInsights": {
113
134
"samplingSettings": {
114
-
"isEnabled": true,
135
+
"isEnabled": false,
115
136
"excludedTypes": "Request"
116
137
},
117
138
"enableLiveMetrics": true
@@ -138,16 +159,20 @@ Logging to Application Insights using lower severity requires an explicit overri
> 📝 **Tip** - The `Microsoft.ApplicationInsights.WorkerService` adjusts logging behavior of the worker (_i.e_ your Function App) to no longer emit logs through the host application (_i.e_ the Functions Runtime host controlling your Function App). Once installed, logs are sent directly to application insights from the worker process instead.
141
163
142
164
7. Open the `Program.cs` and add some using directives at the top of the file:
143
165
144
166
```c#
145
167
using Microsoft.Azure.Functions.Worker;
168
+
using Microsoft.Azure.Functions.Worker.Builder;
169
+
using Microsoft.Extensions.Configuration;
146
170
using Microsoft.Extensions.DependencyInjection;
171
+
using Microsoft.Extensions.Hosting;
147
172
using Microsoft.Extensions.Logging;
148
173
```
149
174
150
-
8. Further down in `Program.cs`, uncomment the relevant portion of the code.
175
+
8. Further down in `Program.cs`, replace the commented code with the relevant portion of the code.
151
176
152
177
*Replace*
153
178
@@ -161,6 +186,15 @@ Logging to Application Insights using lower severity requires an explicit overri
161
186
*With*
162
187
163
188
```c#
189
+
// Reading and configuring log levels and categories
190
+
191
+
var hostJsonLoggingSection = new ConfigurationBuilder()
Along with their messages, those are amongst the most important properties from the collected logs.
209
-
In the next section, you will dive into those properties in a bit more details.
241
+
Along with their messages, the _Severity Level_ and log _Category_ are amongst the most important properties from the collected logs. In the next section, you will dive into those properties in a bit more details.
210
242
211
243
12. Hit <kbd>Ctrl</kbd>+<kbd>C</kbd> from the Console of the running application to stop its execution.
212
244
@@ -219,7 +251,7 @@ You will also learn about _Log Categories_ and how to filter log output based up
219
251
220
252
> 📝 **Tip** - App Insights is a comprehensive Application Performance Monitoring (APM) solution. As such, it does a lot more than collecting traces from a running application. In this lesson, you will mostly focus on interacting with App Insights using .NET's [Microsoft.Extensions.Logging.ILogger](https://learn.microsoft.com/en-us/dotnet/core/extensions/logging) abstraction.
221
253
222
-
As you have seen on the previous section, each log is associated with a set of properties, two of which are its _Category_ and _Log Level_ properties.
254
+
As you have seen on the previous section, each log is associated with a set of properties, two of which are its _CategoryName_ and _Severity Level_ properties.
223
255
224
256
#### Log levels
225
257
@@ -248,6 +280,8 @@ The [ILogger](https://learn.microsoft.com/en-us/dotnet/api/microsoft.extensions.
248
280
249
281
Using `LogLevel.None` effectively disables log output.
250
282
283
+
> 📝 **Tip** - The .NET `ILogger` abstraction defines the various log levels listed above. However, in App Insights, this translates to the concept of _Severity Level_. There is a one-to-one correspondance between the .NET `LogLevel` and App Insights’ _Severity Levels_.
284
+
251
285
In the previous exercise, you have seen how setting the default log level for the entire application to `"Trace"` in the `host.json` file dramatically increased the amount of traces emitted when running the application. Changing the default level is a crude way to limit the quantity of logs. Later in this exercise, you will learn to configure log levels for particular _categories_ of logs.
252
286
253
287
### Categories
@@ -285,8 +319,8 @@ In this exercise, you will discover log categories and learn how to filter log o
285
319
286
320
```sql
287
321
traces
288
-
| summarize count(message) by tostring(customDimensions.Category)
289
-
| order by customDimensions_Category
322
+
| summarize count(message) by tostring(customDimensions.CategoryName)
323
+
| order by customDimensions_CategoryName
290
324
```
291
325
292
326
> 📝 **Tip** - App Insights uses a SQL-like query language named [Kusto Query Language](https://learn.microsoft.com/en-us/azure/data-explorer/kusto/query/) (KQL).
@@ -333,8 +367,8 @@ In this exercise, you will discover log categories and learn how to filter log o
333
367
334
368
```sql
335
369
traces
336
-
| summarize count(message) by tostring(customDimensions.Category)
337
-
| order by customDimensions_Category
370
+
| summarize count(message) by tostring(customDimensions.CategoryName)
371
+
| order by customDimensions_CategoryName
338
372
```
339
373
340
374
> 🔎 **Observation** - After a few minutes, you should see a dramatic reduction in the amount of categories under which your application logs. In fact, given enough time, only logs associated with the `AzFuncUni.Logging.HelloWorldHttpTrigger` category hierarchy should be emitted.
@@ -364,6 +398,7 @@ In this exercise, you will discover log categories and learn how to filter log o
0 commit comments