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
"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.",
25
25
"line": 12
26
26
},
27
+
{
28
+
"title": "Configuring default log levels for the Isolated Worker process",
"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",
"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.",
"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.",
43
55
"line": 6
44
56
},
45
-
{
46
-
"title": "Reading and configuring log levels and categories",
"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.",
"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
56
62
},
57
63
{
58
64
"title": "Remove logging constraints for debugging purposes",
"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.",
Copy file name to clipboardExpand all lines: lessons/dotnet8/logging/README.md
+41-22Lines changed: 41 additions & 22 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -99,9 +99,10 @@ file and relies entirely on environment variables and application settings in Az
99
99
in this lesson, however, it is often desirable to use a separate file for the log configuration.
100
100
101
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.
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.
105
106
106
107
In the following section, you will learn the basics of _Application Insights_ and its _Live Metrics_ dashboard.
107
108
@@ -138,21 +139,48 @@ In the following section, you will learn the basics of _Application Insights_ an
138
139
"enableLiveMetrics": true
139
140
},
140
141
"logLevel": {
141
-
"default": "Trace"
142
+
"default": "Warning"
142
143
}
143
144
}
144
145
}
145
146
```
146
147
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:
> 📝 **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.
148
176
149
177
However, logging to App Insights is not enabled by default.
150
178
151
179
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.
152
180
153
181
Logging to Application Insights using lower severity requires an explicit override.
154
182
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:
@@ -161,7 +189,7 @@ Logging to Application Insights using lower severity requires an explicit overri
161
189
162
190
> 📝 **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.
163
191
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:
165
193
166
194
```c#
167
195
using Microsoft.Azure.Functions.Worker;
@@ -172,7 +200,7 @@ Logging to Application Insights using lower severity requires an explicit overri
172
200
using Microsoft.Extensions.Logging;
173
201
```
174
202
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.
176
204
177
205
*Replace*
178
206
@@ -186,15 +214,6 @@ Logging to Application Insights using lower severity requires an explicit overri
186
214
*With*
187
215
188
216
```c#
189
-
// Reading and configuring log levels and categories
190
-
191
-
var hostJsonLoggingSection = new ConfigurationBuilder()
@@ -214,17 +233,17 @@ Logging to Application Insights using lower severity requires an explicit overri
214
233
});
215
234
```
216
235
217
-
9. Compile and run the application again.
236
+
11. Compile and run the application again.
218
237
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.
220
239
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.
222
241
223
242
> 📝 **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.
224
243
225
244
Once the dashboard displays, notice that your machine is listed as one of the servers currently connected to App Insights.
226
245
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.
228
247
229
248
```http
230
249
POST http://localhost:7071/api/HelloWorldHttpTrigger
@@ -240,7 +259,7 @@ Details from the selected log are displayed on the lower right pane. In particul
240
259
241
260
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.
242
261
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.
0 commit comments