diff options
Diffstat (limited to 'Services')
-rw-r--r-- | Services/LoggerService.cs | 37 |
1 files changed, 37 insertions, 0 deletions
diff --git a/Services/LoggerService.cs b/Services/LoggerService.cs new file mode 100644 index 0000000..dd78e23 --- /dev/null +++ b/Services/LoggerService.cs @@ -0,0 +1,37 @@ +using Microsoft.AspNetCore.Mvc; +using Microsoft.Extensions.Hosting; + +namespace LibraryAPI.Services { + public class LoggerService : IHostedService { + private readonly IWebHostEnvironment env; + private readonly string log_file = "Log.txt"; + private Timer timer; + + public LoggerService(IWebHostEnvironment env) { + this.env = env; + } + + public Task StartAsync(CancellationToken cancel_token) { + timer = new Timer(DoWork, null, TimeSpan.Zero, TimeSpan.FromMinutes(5)); + return Task.CompletedTask; + } + + public void DoWork(object state){ + LogToFile("Executing task\n" + DateTime.Now.ToString("dd/MM/yyyy hh:mm:ss")); + } + + public Task StopAsync(CancellationToken cancel_token) { + LogToFile("Stopping task...\n"); + timer.Dispose(); + return Task.CompletedTask; + } + + private void LogToFile(string message) { + var path = $@"{env.ContentRootPath}/wwwroot/{log_file}"; + + using (StreamWriter w = new StreamWriter(path, append: true)) { + w.WriteLine(message); + } + } + } +} |