Search code examples
c#microsoft-graph-apihttpresponseilogger

how to add file logging to c# graph api notification app (switch or extend Ilogger to log infos to a log-File)


i have started in the last few weeks working (or trying it) Simple MVC-App for notifications.

I can log informations to console but how can I add easy an file logger for the notification function for http-requests?. From the scratch implementing with very much work I can realize it, but without any supporting functions for filelogging. I found the Ilogger to log in the Console. But is there an easy way to switch Ilogger from logging to console logging to file?

  public class NotificationsController : ControllerBase
  {
    private readonly MyConfig config;
    private readonly ILogger<NotificationsController> _logger;

  public NotificationsController(MyConfig config, ILogger<NotificationsController> logger)
  {
      this.config = config;
            _logger = logger;
  }

    [HttpGet]
    public ActionResult<string> Get()
    {

               ...

            string Message = $"About page visited at {DateTime.UtcNow.ToLongTimeString()}";
            _logger.LogInformation(Message);

              ...

The example I modified is fromn here: https://learn.microsoft.com/de-de/learn/modules/msgraph-changenotifications-trackchanges/5-exercise-change-notification

https://github.com/microsoftgraph/msgraph-training-changenotifications/tree/live


Solution

  • Thank you for reaching out. AS.NET Core doesn't include a logging provider for writing logs to files, see documentation on logging in .NET Core and ASP.NET. To write logs to files, consider using a third party logging provider.

    Let me know whether this helps and if you have further questions.