Skip to content

Commit b226ec9

Browse files
authored
Merge pull request #106559 from jeffhollan/di-logging
functions: clarifying log categories
2 parents 5a9ae91 + ee771e6 commit b226ec9

File tree

1 file changed

+46
-0
lines changed

1 file changed

+46
-0
lines changed

articles/azure-functions/functions-dotnet-dependency-injection.md

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,52 @@ If you need your own logging provider, register a custom type as an `ILoggerProv
127127
> - Do not add `AddApplicationInsightsTelemetry()` to the services collection as it registers services that conflict with services provided by the environment.
128128
> - Do not register your own `TelemetryConfiguration` or `TelemetryClient` if you are using the built-in Application Insights functionality. If you need to configure your own `TelemetryClient` instance, create one via the injected `TelemetryConfiguration` as shown in [Monitor Azure Functions](./functions-monitoring.md#version-2x-and-later-2).
129129
130+
### ILogger<T> and ILoggerFactory
131+
132+
The host will inject `ILogger<T>` and `ILoggerFactory` services into constructors. However, by default these new logging filters will be filtered out of the function logs. You will need to modify the `host.json` file to opt into additional filters and categories. The following sample demonstrates adding an `ILogger<HttpTrigger>` with logs that will be exposed by the host.
133+
134+
```csharp
135+
namespace MyNamespace
136+
{
137+
public class HttpTrigger
138+
{
139+
private readonly ILogger<HttpTrigger> _log;
140+
141+
public HttpTrigger(ILogger<HttpTrigger> log)
142+
{
143+
_log = log;
144+
}
145+
146+
[FunctionName("HttpTrigger")]
147+
public async Task<IActionResult> Run(
148+
[HttpTrigger(AuthorizationLevel.Anonymous, "get", "post", Route = null)] HttpRequest req)
149+
{
150+
_log.LogInformation("C# HTTP trigger function processed a request.");
151+
152+
// ...
153+
}
154+
}
155+
```
156+
157+
And a `host.json` file that adds the log filter.
158+
159+
```json
160+
{
161+
"version": "2.0",
162+
"logging": {
163+
"applicationInsights": {
164+
"samplingExcludedTypes": "Request",
165+
"samplingSettings": {
166+
"isEnabled": true
167+
}
168+
},
169+
"logLevel": {
170+
"MyNamespace.HttpTrigger": "Information"
171+
}
172+
}
173+
}
174+
```
175+
130176
## Function app provided services
131177

132178
The function host registers many services. The following services are safe to take as a dependency in your application:

0 commit comments

Comments
 (0)