Skip to content

Commit 465cacf

Browse files
committed
proof-proof reading again !
1 parent 9559aef commit 465cacf

File tree

7 files changed

+68
-43
lines changed

7 files changed

+68
-43
lines changed

.tours/dotnet8/logging/.tours/02-logging-to-application-insights.tour

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,23 @@
1919
"line": 9
2020
},
2121
{
22-
"title": "Configuring the log levels",
22+
"title": "Configuring default logging levels for the Functions Runtime host",
2323
"file": "../../../src/dotnet8/logging/AzFuncUni.Logging/host.json",
24-
"description": "The Functions Runtime host uses the `host.json` file to configure configure logging for its own activity, including the behaviour of your input bindings.\r\n\r\nIn this lesson, were are also sharing the `host.json` log configuration for the worker process itself. That’s where you define the log level associated with each category.",
24+
"description": "The Functions Runtime host uses the `host.json` file to configure logging for its own activity, including the behaviour of your input bindings.",
2525
"line": 12
2626
},
27+
{
28+
"title": "Configuring default log levels for the Isolated Worker process",
29+
"file": "../../../src/dotnet8/logging/AzFuncUni.Logging/appsettings.json",
30+
"description": "The Isolated Worker process uses is own – separate – `appsettings.json` configuration file to configure logging.\r\n\r\nThat’s where you define the log level associated with each category.",
31+
"line": 4
32+
},
33+
{
34+
"title": "Copying the `appsettings.json` to the output folder",
35+
"file": "../../../src/dotnet8/logging/AzFuncUni.Logging/AzFuncUni.Logging.csproj",
36+
"description": "To register the `appsettings.json` as the source of configuration for the worker process, this file needs to be copied to the output folder alonside your compiled binaries for the worker process.",
37+
"line": 21
38+
},
2739
{
2840
"title": "Installing Application Insights dependencies",
2941
"file": "../../../src/dotnet8/logging/AzFuncUni.Logging/AzFuncUni.Logging.csproj",
@@ -42,23 +54,17 @@
4254
"description": "These _using_ statements at the top of the source file are required to enable logging to Application Insights.\r\n\r\nNote that most of those statements are generic-enough; that’s because most features are packaged using extension classes located in a limited number of well-known namespaces.",
4355
"line": 6
4456
},
45-
{
46-
"title": "Reading and configuring log levels and categories",
47-
"file": "../../../src/dotnet8/logging/AzFuncUni.Logging/Program.cs",
48-
"description": "Those instructions load the `logging` section from the `host.json` configuration file into a temporary variable.Then, passing this variable to `Logging.AddConfiguration()` configures log levels and categories accordingly.",
49-
"line": 19
50-
},
5157
{
5258
"title": "Logging to Application Insights",
5359
"file": "../../../src/dotnet8/logging/AzFuncUni.Logging/Program.cs",
5460
"description": "Those instructions enable logging to ApplicationInsights.\r\n\r\nThe `AddApplicationInsightsTelemetryWorkerService` method enables connection to Application Insights from _worker_ processes such as non-HTTP workloads, background tasks and console applications and is not specific to Function Apps.\r\n\r\nThe `ConfigureFunctionsApplicationInsights` method is a simple method provided by the Azure Functions .NET Worker SDK to properly setup the Function App for logging to Application Insights.",
55-
"line": 25
61+
"line": 17
5662
},
5763
{
5864
"title": "Remove logging constraints for debugging purposes",
5965
"file": "../../../src/dotnet8/logging/AzFuncUni.Logging/Program.cs",
6066
"description": "In order to help reduce cost of your cloud infrastructure, the Application Insights SDK adds a default logging filter that instructs `ILogger` to capture logs with a severity of `Warning` or more.\r\n\r\nIn this lesson, that rule is removed so that logging using lower levels, such as `Information` can be sent and recorded into Application Insights.",
61-
"line": 34
67+
"line": 28
6268
},
6369
{
6470
"title": "Selecting a category for logging",

lessons/dotnet8/logging/README.md

Lines changed: 41 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -99,9 +99,10 @@ file and relies entirely on environment variables and application settings in Az
9999
in this lesson, however, it is often desirable to use a separate file for the log configuration.
100100
101101
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.
102+
it needs its own file to store its own settings and configurations for logging. By default, if
103+
a file named `appsettings.json` is present in the output folder, it can automatically registered
104+
as a source of configuration for the worker. Additionally, the log levels and categories
105+
can be automatically configured from the `Logging/LogLevel` configuration section.
105106
106107
In the following section, you will learn the basics of _Application Insights_ and its _Live Metrics_ dashboard.
107108
@@ -138,21 +139,48 @@ In the following section, you will learn the basics of _Application Insights_ an
138139
"enableLiveMetrics": true
139140
},
140141
"logLevel": {
141-
"default": "Trace"
142+
"default": "Warning"
142143
}
143144
}
144145
}
145146
```
146147

147-
Our changes enable integration with the _Live Metrics_ dashboard associated with the _Application Insights_ resource that we will refer to from now on as “App Insights”, for short. We also have lowered the default _Log Level_ to `Trace` so that virtually every log emitted from the application goes out unfiltered.
148+
Our changes enable integration with the _Live Metrics_ dashboard associated with the _Application Insights_ resource that we will refer to from now on as “App Insights”, for short.
149+
150+
6. At the root of your project, create a new file named `appsettings.json` with the following content:
151+
152+
```json
153+
{
154+
"Logging": {
155+
"LogLevel": {
156+
"Default": "Trace",
157+
}
158+
}
159+
}
160+
```
161+
162+
> 📝 **Tip** - Please, make sure to name the file `appsettings.json` exactly.
163+
164+
7. Open the `AzFuncUni.Logging.csproj` project file, locate the `<None Update="host.json">` XML start element
165+
somewhat towards the end of the file and add the following – mostly identical section for `appsettings.json` like so:
166+
167+
```xml
168+
<None Update="appsettings.json">
169+
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
170+
</None>
171+
```
172+
173+
> 📝 **Tip** - Please, note that this section is very similar to the one that copies the `host.json` file to the output folder.
174+
175+
The `appsettings.json` configuration file defines the default level for logging to `Trace` which allows virtually all logs emitted from your worker process to be sent to App Insights.
148176

149177
However, logging to App Insights is not enabled by default.
150178

151179
Furthermore, in an effort to help manage your infrastructure costs efficiently, the App Insights SDK adds a default logging filter for capturing warnings and errors only.
152180

153181
Logging to Application Insights using lower severity requires an explicit override.
154182

155-
6. To fix this, install the [required packages](https://learn.microsoft.com/fr-fr/azure/azure-functions/dotnet-isolated-process-guide?tabs=hostbuilder%2Cwindows#install-packages) as follows:
183+
8. To fix this, install the [required packages](https://learn.microsoft.com/fr-fr/azure/azure-functions/dotnet-isolated-process-guide?tabs=hostbuilder%2Cwindows#install-packages) as follows:
156184

157185
```pwsh
158186
dotnet add package Microsoft.ApplicationInsights.WorkerService
@@ -161,7 +189,7 @@ Logging to Application Insights using lower severity requires an explicit overri
161189
162190
> 📝 **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.
163191
164-
7. Open the `Program.cs` and add some using directives at the top of the file:
192+
9. Open the `Program.cs` and add some using directives at the top of the file:
165193
166194
```c#
167195
using Microsoft.Azure.Functions.Worker;
@@ -172,7 +200,7 @@ Logging to Application Insights using lower severity requires an explicit overri
172200
using Microsoft.Extensions.Logging;
173201
```
174202
175-
8. Further down in `Program.cs`, replace the commented code with the relevant portion of the code.
203+
10. Further down in `Program.cs`, replace the commented code with the relevant portion of the code.
176204
177205
*Replace*
178206
@@ -186,15 +214,6 @@ Logging to Application Insights using lower severity requires an explicit overri
186214
*With*
187215
188216
```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-
198217
// Logging to Application Insights
199218
200219
builder.Services
@@ -214,17 +233,17 @@ Logging to Application Insights using lower severity requires an explicit overri
214233
});
215234
```
216235
217-
9. Compile and run the application again.
236+
11. Compile and run the application again.
218237
219-
10. From the Azure Portal, navigate to App Insights and display the _Live Metrics_ dashboard. You can find `Live Metrics` as one of the options available on the left pane, under the `Investigate` topic.
238+
12. From the Azure Portal, navigate to App Insights and display the _Live Metrics_ dashboard. You can find `Live Metrics` as one of the options available on the left pane, under the `Investigate` topic.
220239
221-
Wait a couple dozen of seconds for the web page to refresh and display the dashboard.
240+
Wait for ten to twenty seconds for the web page to refresh and display the dashboard.
222241
223242
> 📝 **Tip** - Some ad blockers are known to prevent the dashboard from displaying. If you have μBlock₀, you may see a `Data is temporarily inaccessible` red banner, for instance. Make sure to disable your ad blocker for the _Live Metrics_ page to display properly.
224243
225244
Once the dashboard displays, notice that your machine is listed as one of the servers currently connected to App Insights.
226245
227-
11. On your local machine, call the HTTP-triggered function a couple of times.
246+
13. On your local machine, call the HTTP-triggered function a couple of times.
228247
229248
```http
230249
POST http://localhost:7071/api/HelloWorldHttpTrigger
@@ -240,7 +259,7 @@ Details from the selected log are displayed on the lower right pane. In particul
240259
241260
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.
242261
243-
12. Hit <kbd>Ctrl</kbd>+<kbd>C</kbd> from the Console of the running application to stop its execution.
262+
14. Hit <kbd>Ctrl</kbd>+<kbd>C</kbd> from the Console of the running application to stop its execution.
244263
245264
## 3. Log levels and categories
246265
-13.6 KB
Loading

src/dotnet8/logging/AzFuncUni.Logging/AzFuncUni.Logging.csproj

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@
1616
<PackageReference Include="Microsoft.Azure.Functions.Worker.Sdk" Version="2.0.0" />
1717
</ItemGroup>
1818
<ItemGroup>
19+
<None Update="appsettings.json">
20+
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
21+
</None>
1922
<None Update="host.json">
2023
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
2124
</None>

src/dotnet8/logging/AzFuncUni.Logging/Program.cs

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,6 @@
99

1010
builder.ConfigureFunctionsWebApplication();
1111

12-
// Reading and configuring log levels and categories
13-
14-
var hostJsonLoggingSection = new ConfigurationBuilder()
15-
.AddJsonFile("host.json")
16-
.Build()
17-
.GetSection("logging");
18-
19-
builder.Logging.AddConfiguration(hostJsonLoggingSection);
20-
2112
// Logging to Application Insights
2213

2314
builder.Services
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"logging": {
3+
"logLevel": {
4+
"default": "Trace"
5+
}
6+
}
7+
}

src/dotnet8/logging/AzFuncUni.Logging/host.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,7 @@
99
"enableLiveMetrics": true
1010
},
1111
"logLevel": {
12-
"default": "Warning",
13-
"AzFuncUni.Logging.HelloWorldHttpTrigger": "Information"
12+
"default": "Warning"
1413
}
1514
}
1615
}

0 commit comments

Comments
 (0)