forked from ProfessionalCSharp/ProfessionalCSharp7
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSampleController.cs
36 lines (33 loc) · 1.2 KB
/
SampleController.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
using Microsoft.Extensions.Logging;
using System;
using System.Net.Http;
using System.Threading.Tasks;
namespace LoggingScopeSample
{
class SampleController
{
private readonly ILogger<SampleController> _logger;
public SampleController(ILogger<SampleController> logger)
{
_logger = logger;
_logger.LogTrace("ILogger injected into {0}", nameof(SampleController));
}
public async Task NetworkRequestSampleAsync(string url)
{
using (_logger.BeginScope("NetworkRequestSampleAsync, url: {0}", url))
{
try
{
_logger.LogInformation(LoggingEvents.Networking, "Started");
var client = new HttpClient();
string result = await client.GetStringAsync(url);
_logger.LogInformation(LoggingEvents.Networking, "Completed with characters {0} received", result.Length);
}
catch (Exception ex)
{
_logger.LogError(LoggingEvents.Networking, ex, "Error, error message: {0}, HResult: {1}", ex.Message, ex.HResult);
}
}
}
}
}