Skip to content

Commit ad774fc

Browse files
committed
[lesson] proof-reading
1 parent c7ff83e commit ad774fc

File tree

2 files changed

+57
-22
lines changed

2 files changed

+57
-22
lines changed

lessons/dotnet8/logging/README.md

Lines changed: 57 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,10 @@ This lesson consists of the following exercises:
2525

2626
| Prerequisite | Exercise
2727
| - | -
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
3232

3333
See [.NET 8 prerequisites](../prerequisites/README.md) for more details.
3434

@@ -77,32 +77,53 @@ This exercise is a condensed version of the
7777
7878
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.
7979
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.
8181
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+
![](./isolated-worker-process.png)
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.
83107
84108
### Steps
85109
86110
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.
87111
88112
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.
89113
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.
91115
92116
4. Back to your local working folder, open the `local.settings.json` project file and add two corresponding properties in the `Values` section:
93117
94118
```json
95119
{
96120
"Values": {
97121
98-
"APPINSIGHTS_INSTRUMENTATIONKEY": "<paste-the-instrumentation-key>",
99122
"APPLICATIONINSIGHTS_CONNECTION_STRING": "<paste-the-connection-string>"
100123
}
101124
}
102125
```
103126

104-
> 📝 **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-
106127
5. Open the `host.json` file and replace its content with:
107128

108129
```json
@@ -111,7 +132,7 @@ In this section, you will learn the basics of _Application Insights_ and its _Li
111132
"logging": {
112133
"applicationInsights": {
113134
"samplingSettings": {
114-
"isEnabled": true,
135+
"isEnabled": false,
115136
"excludedTypes": "Request"
116137
},
117138
"enableLiveMetrics": true
@@ -138,16 +159,20 @@ Logging to Application Insights using lower severity requires an explicit overri
138159
dotnet add package Microsoft.Azure.Functions.Worker.ApplicationInsights
139160
```
140161
162+
> 📝 **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.
141163
142164
7. Open the `Program.cs` and add some using directives at the top of the file:
143165
144166
```c#
145167
using Microsoft.Azure.Functions.Worker;
168+
using Microsoft.Azure.Functions.Worker.Builder;
169+
using Microsoft.Extensions.Configuration;
146170
using Microsoft.Extensions.DependencyInjection;
171+
using Microsoft.Extensions.Hosting;
147172
using Microsoft.Extensions.Logging;
148173
```
149174
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.
151176
152177
*Replace*
153178
@@ -161,6 +186,15 @@ Logging to Application Insights using lower severity requires an explicit overri
161186
*With*
162187
163188
```c#
189+
// Reading and configuring log levels and categories
190+
191+
var hostJsonLoggingSection = new ConfigurationBuilder()
192+
.AddJsonFile("host.json")
193+
.Build()
194+
.GetSection("logging");
195+
196+
builder.Logging.AddConfiguration(hostJsonLoggingSection);
197+
164198
// Logging to Application Insights
165199
166200
builder.Services
@@ -200,13 +234,11 @@ Once the dashboard displays, notice that your machine is listed as one of the se
200234
201235
From the right pane, locate and click on one of the recorded logs, with the following message text: `"C# HTTP trigger function processed a request"`.
202236
203-
Details from the selected log are displayed on the lower right pane. In particular, notice the following two properties:
237+
Details from the selected log are displayed on the lower right pane. In particular, notice the following property:
204238
205-
- `Category`: `Function.HelloWorldHttpTrigger.User`
206-
- `LogLevel`: `Information`
239+
- `CategoryName`: `AzFuncUni.Logging.HelloWorldHttpTrigger`
207240
208-
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.
210242
211243
12. Hit <kbd>Ctrl</kbd>+<kbd>C</kbd> from the Console of the running application to stop its execution.
212244
@@ -219,7 +251,7 @@ You will also learn about _Log Categories_ and how to filter log output based up
219251
220252
> 📝 **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.
221253
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.
223255
224256
#### Log levels
225257
@@ -248,6 +280,8 @@ The [ILogger](https://learn.microsoft.com/en-us/dotnet/api/microsoft.extensions.
248280
249281
Using `LogLevel.None` effectively disables log output.
250282
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+
251285
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.
252286
253287
### Categories
@@ -285,8 +319,8 @@ In this exercise, you will discover log categories and learn how to filter log o
285319
286320
```sql
287321
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
290324
```
291325
292326
> 📝 **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
333367
334368
```sql
335369
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
338372
```
339373
340374
> 🔎 **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
364398
365399
"AzureFunctionsJobHost__Logging__LogLevel__default": "Debug",
366400
"AzureFunctionsJobHost__Logging__LogLevel__Function__HelloWorldHttpTrigger": "Trace",
401+
"Logging__LogLevel__Default": "Warning",
367402
"Logging__LogLevel__AzFuncUni__Logging__HelloWorldHttpTrigger": "Trace"
368403
}
369404
}
30.1 KB
Loading

0 commit comments

Comments
 (0)