Skip to content

Instantly share code, notes, and snippets.

@kadukf
Created January 8, 2020 09:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kadukf/1b5a835c1169cde3318a56a3723f7c71 to your computer and use it in GitHub Desktop.
Save kadukf/1b5a835c1169cde3318a56a3723f7c71 to your computer and use it in GitHub Desktop.
Sample controller with logging and using scopes
[Route("api/[controller]")]
[ApiController]
public class ValuesController : ControllerBase
{
private readonly ILogger<ValuesController> _logger;
public ValuesController(ILogger<ValuesController> logger)
{
_logger = logger ?? throw new ArgumentNullException(nameof(logger));
}
// GET api/values/some_name
[HttpGet("{userId}")]
public ActionResult<string> Get(string userId)
{
using (_logger.BeginScope("my scope"))
{
_logger.LogInformation("Handling sample request");
return "Hello " + userId;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment