diff options
author | HombreLaser <sebastian-440@live.com> | 2022-11-27 13:37:06 -0600 |
---|---|---|
committer | HombreLaser <sebastian-440@live.com> | 2022-11-27 13:37:06 -0600 |
commit | 6f263df59a895b6bcc2fe22b9626ddd81537c217 (patch) | |
tree | a7b93ea9111d28f00c7f1dccf9a206058c12a03c /Jobs/LoggerJob.cs | |
parent | 3da900a30e788d0acf2fcee7dba2aecdb16aab43 (diff) |
Añadido job de loggeo
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 |