summaryrefslogtreecommitdiff
path: root/Services/LoggerService.cs
diff options
context:
space:
mode:
authorHombreLaser <sebastian-440@live.com>2022-10-26 18:10:07 -0500
committerHombreLaser <sebastian-440@live.com>2022-10-26 18:10:07 -0500
commit5c57b4411f7f4c5a244394579b2e5af4f946b389 (patch)
tree26041eca87dd69b6eaee7f41e6d1e6accb3ed2fb /Services/LoggerService.cs
parent0a04f3a9f516754a9e634bbe0322cd947d63c4a5 (diff)
Añadido servicio logger
Diffstat (limited to 'Services/LoggerService.cs')
-rw-r--r--Services/LoggerService.cs37
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);
+ }
+ }
+ }
+}