diff options
Diffstat (limited to 'Jobs/LoggerJob.cs')
-rw-r--r-- | Jobs/LoggerJob.cs | 36 |
1 files changed, 36 insertions, 0 deletions
diff --git a/Jobs/LoggerJob.cs b/Jobs/LoggerJob.cs new file mode 100644 index 0000000..72a571f --- /dev/null +++ b/Jobs/LoggerJob.cs @@ -0,0 +1,36 @@ +// Singleton service. +namespace BackendPIA.Jobs { + public class LoggerJob : IHostedService { + private Timer? _timer; + private readonly IWebHostEnvironment _env; + private readonly string _log_file = "Log.txt"; + + public LoggerJob(IWebHostEnvironment env) { + _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); + } + } + } +}
\ No newline at end of file |