Skip to content

Commit

Permalink
+semver: minor | added logging service as a configuration that will b…
Browse files Browse the repository at this point in the history
…e enabled/disabled via env variable (#91)

* +semver: minor | added logging service as a configuration that will be enabled/disabled via env variable

* removed entry for appsettings.json in csproj file

* +semver: chore | updated readme and removed Microsoft.Extensions.Configuration.Json Nuget package

---------

Co-authored-by: Mohammed Owes <[email protected]>
  • Loading branch information
md-owes and Mohammed Owes authored Aug 2, 2023
1 parent d644c35 commit f2ffc7e
Show file tree
Hide file tree
Showing 7 changed files with 83 additions and 15 deletions.
19 changes: 19 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,25 @@ For MacOS, use the package file named directory-deleter-v{VERSION}.pkg, for Wind

To install directory-deleter on Windows 10 & 11, you need to install the public key certificate first. The certificate is named directory-deleter.cer and it should be installed in Trusted People as mentioned in [Microsoft documentation](https://learn.microsoft.com/en-us/dotnet/maui/windows/deployment/publish-cli#installing-the-app) for store apps

To enable logging of issues in directory-deleter
* On Mac, you need to execute the following command in terminal
```
launchctl setenv DD_EnableLogs 1
```
* On windows, you need to set system environment variable either manually or via below command
```
setx DD_EnableLogs "1" /M
```
Now you will see a new file created at the following locations
* On Mac, file named directory-delete<date>.log will appear in /Users/<username>/Library
* On Windows, file named directory-delete<date>.log
* if it is exe then location will be C:\Users\<username>\AppData\Roaming\<username>\c5a240f0-6866-4aa3-8d34-9c682b0cf217\Data
* If it is msix then location will be C:\Users\<username>\AppData\Local\Packages\c5a240f0-6866-4aa3-8d34-9c682b0cf217_<packageid>\LocalState
Note: On Windows the log location will have a static GUID (c5a240f0-6866-4aa3-8d34-9c682b0cf217) in its path, this GUID is a hardcoded GUID for this app and can change in future.
After changing the value of environment variable, you need to close and open the app again for changes to take effect.
## Usage/Examples
Enter the required information
Expand Down
29 changes: 23 additions & 6 deletions directory-deleter/App.xaml.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,28 @@
namespace directory_deleter;
using directory_deleter.Services;
using Serilog;

namespace directory_deleter;

public partial class App : Application
{
public App()
{
InitializeComponent();
private readonly ISettingsService _service;
public App(IServiceProvider provider)
{
InitializeComponent();
MainPage = new AppShell();

MainPage = new AppShell();
}
_service = provider.GetService<ISettingsService>();
RegisterLogger();
}
private void RegisterLogger()
{
if (_service.EnableLogs)
{
Log.Logger = new LoggerConfiguration()
.MinimumLevel.Debug()
.WriteTo.File(Path.Combine(FileSystem.AppDataDirectory, "directory-delete.log"),
rollingInterval: RollingInterval.Day)
.CreateLogger();
}
}
}
25 changes: 16 additions & 9 deletions directory-deleter/MauiProgram.cs
Original file line number Diff line number Diff line change
@@ -1,30 +1,37 @@
using CommunityToolkit.Maui;
using Serilog;
using directory_deleter.Services;
using Microsoft.Extensions.Configuration;

namespace directory_deleter;

public static class MauiProgram
{
public static MauiApp CreateMauiApp()
{
#if DEBUG
Log.Logger = new LoggerConfiguration()
.MinimumLevel.Debug()
.WriteTo.File(Path.Combine(FileSystem.AppDataDirectory, "directory-delete.log"), rollingInterval: RollingInterval.Day)
.CreateLogger();
Console.WriteLine($"App Installed Location is {FileSystem.AppDataDirectory}");
#endif

var builder = MauiApp.CreateBuilder();
builder
.UseMauiApp<App>()
.UseMauiCommunityToolkit()
.RegisterServices()
.ConfigureFonts(fonts =>
{
fonts.AddFont("OpenSans-Regular.ttf", "OpenSansRegular");
fonts.AddFont("OpenSans-Semibold.ttf", "OpenSansSemibold");
});

IConfiguration config = new ConfigurationBuilder()
.AddEnvironmentVariables()
.Build();

builder.Configuration.AddConfiguration(config);

return builder.Build();
}

public static MauiAppBuilder RegisterServices(this MauiAppBuilder mauiAppBuilder)
{
mauiAppBuilder.Services.AddScoped<ISettingsService, SettingsService>();

return mauiAppBuilder;
}
}
7 changes: 7 additions & 0 deletions directory-deleter/Services/ISettingsService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace directory_deleter.Services
{
public interface ISettingsService
{
bool EnableLogs { get; set; }
}
}
2 changes: 2 additions & 0 deletions directory-deleter/Services/NotificationService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,9 @@ public void Show(string message, CancellationToken token)
#if (!WINDOWS && !MACCATALYST && !MACOS)
ShowWindowsNotification("Directory Deleter", message);
#else
#pragma warning disable CS4014
ShowMauiNotification(message, token);
#pragma warning restore CS4014
#endif
}
}
Expand Down
14 changes: 14 additions & 0 deletions directory-deleter/Services/SettingsService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using Microsoft.Extensions.Configuration;

namespace directory_deleter.Services
{
public class SettingsService : ISettingsService
{
public bool EnableLogs { get; set; }

public SettingsService()
{
EnableLogs = Environment.GetEnvironmentVariable("DD_EnableLogs") == "1";
}
}
}
2 changes: 2 additions & 0 deletions directory-deleter/directory-deleter.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,8 @@
<ItemGroup>
<PackageReference Include="CommunityToolkit.Maui" Version="5.2.0" />
<PackageReference Include="CommunityToolkit.Mvvm" Version="8.2.1" />
<PackageReference Include="Microsoft.Extensions.Configuration.Binder" Version="7.0.4" />
<PackageReference Include="Microsoft.Extensions.Configuration.EnvironmentVariables" Version="7.0.0" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
<PackageReference Include="Serilog.Extensions.Logging" Version="7.0.0" />
<PackageReference Include="Serilog.Sinks.File" Version="5.0.0" />
Expand Down

0 comments on commit f2ffc7e

Please sign in to comment.