-
Notifications
You must be signed in to change notification settings - Fork 45
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: core logging functionality implemented
- Loading branch information
Showing
17 changed files
with
234 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
|
||
namespace Project2FA.Services.Logging | ||
{ | ||
public interface ILoggingService | ||
{ | ||
Task Log(string message); | ||
Task LogException(Exception exc); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
using Project2FA.Core; | ||
using Project2FA.Services.Enums; | ||
using System; | ||
using System.Threading.Tasks; | ||
using UNOversal.Services.File; | ||
using Windows.Storage; | ||
|
||
namespace Project2FA.Services.Logging | ||
{ | ||
/// <summary> | ||
/// Service for logging of exception and custom messages | ||
/// </summary> | ||
public class LoggingService : ILoggingService | ||
{ | ||
private string _timeStemp = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + " - "; | ||
|
||
private IFileService FileService { get; } | ||
|
||
public LoggingService(IFileService fileService) | ||
{ | ||
FileService = fileService; | ||
} | ||
|
||
/// <summary> | ||
/// Log a custom message | ||
/// </summary> | ||
/// <param name="message"></param> | ||
/// <returns></returns> | ||
public async Task Log(string message) | ||
{ | ||
if (SettingsService.Instance.LoggingSetting == LoggingPreferEnum.Full) | ||
{ | ||
if(await FileService.FileExistsAsync(Constants.LogName, ApplicationData.Current.LocalFolder)) | ||
{ | ||
var file = await ApplicationData.Current.LocalFolder.GetFileAsync(Constants.LogName); | ||
await FileIO.AppendTextAsync(file, _timeStemp + message); | ||
} | ||
else | ||
{ | ||
var file = await ApplicationData.Current.LocalFolder.CreateFileAsync(Constants.LogName); | ||
await FileIO.AppendTextAsync(file, _timeStemp + message); | ||
} | ||
} | ||
} | ||
|
||
/// <summary> | ||
/// Log a exception | ||
/// </summary> | ||
/// <param name="exc"></param> | ||
/// <returns></returns> | ||
public async Task LogException(Exception exc) | ||
{ | ||
if (SettingsService.Instance.LoggingSetting == LoggingPreferEnum.Simple || | ||
SettingsService.Instance.LoggingSetting == LoggingPreferEnum.Full) | ||
{ | ||
if (await FileService.FileExistsAsync(Constants.LogName, ApplicationData.Current.LocalFolder)) | ||
{ | ||
var file = await ApplicationData.Current.LocalFolder.GetFileAsync(Constants.LogName); | ||
await WriteExceptionLog(exc, file); | ||
|
||
} | ||
else | ||
{ | ||
var file = await ApplicationData.Current.LocalFolder.CreateFileAsync(Constants.LogName); | ||
await WriteExceptionLog(exc, file); | ||
} | ||
} | ||
} | ||
|
||
/// <summary> | ||
/// Write the exception to a file | ||
/// </summary> | ||
/// <param name="exc"></param> | ||
/// <param name="file"></param> | ||
/// <returns></returns> | ||
private async Task WriteExceptionLog(Exception exc, StorageFile file) | ||
{ | ||
await FileIO.AppendTextAsync(file, _timeStemp + exc.Source + " - " + exc.Message + "\n"); | ||
if (exc.InnerException != null) | ||
{ | ||
await FileIO.AppendTextAsync(file, "InnerException - " + exc.InnerException.ToString() + "\n"); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.