Skip to content

Commit

Permalink
Add option to change logging level, and default to Info
Browse files Browse the repository at this point in the history
I'm not having to debug issues from people's log files any more...

Fixes #370
  • Loading branch information
canton7 committed May 25, 2017
1 parent 29c7e43 commit 48a6f3c
Show file tree
Hide file tree
Showing 9 changed files with 127 additions and 11 deletions.
2 changes: 1 addition & 1 deletion src/SyncTrayzor/Bootstrapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ protected override void Configure()

var logger = LogManager.GetCurrentClassLogger();
var assembly = this.Container.Get<IAssemblyProvider>();
logger.Debug("SyncTrazor version {0} ({1}) started at {2} (.NET version: {3})", assembly.FullVersion, assembly.ProcessorArchitecture, assembly.Location, DotNetVersionFinder.FindDotNetVersion());
logger.Info("SyncTrazor version {0} ({1}) started at {2} (.NET version: {3})", assembly.FullVersion, assembly.ProcessorArchitecture, assembly.Location, DotNetVersionFinder.FindDotNetVersion());

// This needs to happen before anything which might cause the unhandled exception stuff to be shown, as that wants to know
// where to find the log file.
Expand Down
8 changes: 7 additions & 1 deletion src/SyncTrayzor/Pages/Settings/SettingsView.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -213,11 +213,17 @@
<TabItem Header="{l:Loc SettingsView_Tab_Logging}">
<DockPanel>
<GroupBox DockPanel.Dock="Bottom" Header="{l:Loc SettingsView_Section_SyncTrayzor}">
<Button DockPanel.Dock="Top" Margin="0,10,0,0" Padding="5,0"
<DockPanel>
<StackPanel DockPanel.Dock="Top" Orientation="Horizontal">
<Label Content="{l:Loc SettingsView_Logging_LogLevel}"/>.
<ComboBox ItemsSource="{Binding LogLevels}" SelectedValuePath="Value" SelectedValue="{Binding SelectedLogLevel.Value}"/>
</StackPanel>
<Button DockPanel.Dock="Top" Margin="0,10,0,0" Padding="5,0"
Command="{s:Action ShowSyncTrayzorLogFile}"
HorizontalAlignment="Left"
Style="{StaticResource DialogButton}"
Content="{l:Loc SettingsView_Logging_ShowSyncTrayzorLog}"/>
</DockPanel>
</GroupBox>

<GroupBox Header="{l:Loc SettingsView_Section_Syncthing}">
Expand Down
27 changes: 19 additions & 8 deletions src/SyncTrayzor/Pages/Settings/SettingsViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,9 @@ public class SettingsViewModel : Screen

public BindableCollection<DebugFacilitySetting> SyncthingDebugFacilities { get; } = new BindableCollection<DebugFacilitySetting>();

public BindableCollection<LabelledValue<LogLevel>> LogLevels { get; }
public SettingItem<LogLevel> SelectedLogLevel { get; set; }

public SettingsViewModel(
IConfigurationProvider configurationProvider,
IAutostartProvider autostartProvider,
Expand Down Expand Up @@ -180,6 +183,22 @@ public SettingsViewModel(
this.SyncthingDenyUpgrade = this.CreateBasicSettingItem(x => x.SyncthingDenyUpgrade);
this.SyncthingDenyUpgrade.RequiresSyncthingRestart = true;

this.PriorityLevels = new BindableCollection<LabelledValue<SyncthingPriorityLevel>>()
{
LabelledValue.Create(Resources.SettingsView_Syncthing_ProcessPriority_AboveNormal, Services.Config.SyncthingPriorityLevel.AboveNormal),
LabelledValue.Create(Resources.SettingsView_Syncthing_ProcessPriority_Normal, Services.Config.SyncthingPriorityLevel.Normal),
LabelledValue.Create(Resources.SettingsView_Syncthing_ProcessPriority_BelowNormal, Services.Config.SyncthingPriorityLevel.BelowNormal),
LabelledValue.Create(Resources.SettingsView_Syncthing_ProcessPriority_Idle, Services.Config.SyncthingPriorityLevel.Idle),
};

this.LogLevels = new BindableCollection<LabelledValue<LogLevel>>()
{
LabelledValue.Create(Resources.SettingsView_Logging_LogLevel_Info, LogLevel.Info),
LabelledValue.Create(Resources.SettingsView_Logging_LogLevel_Debug, LogLevel.Debug),
LabelledValue.Create(Resources.SettingsView_Logging_LogLevel_Trace, LogLevel.Trace),
};
this.SelectedLogLevel = this.CreateBasicSettingItem(x => x.LogLevel);

var configuration = this.configurationProvider.Load();

foreach (var settingItem in this.settings)
Expand All @@ -193,14 +212,6 @@ public SettingsViewModel(
folderSetting.Bind(s => s.IsNotified, (o, e) => this.UpdateAreAllFoldersNotified());
}

this.PriorityLevels = new BindableCollection<LabelledValue<SyncthingPriorityLevel>>()
{
LabelledValue.Create(Resources.SettingsView_Syncthing_ProcessPriority_AboveNormal, Services.Config.SyncthingPriorityLevel.AboveNormal),
LabelledValue.Create(Resources.SettingsView_Syncthing_ProcessPriority_Normal, Services.Config.SyncthingPriorityLevel.Normal),
LabelledValue.Create(Resources.SettingsView_Syncthing_ProcessPriority_BelowNormal, Services.Config.SyncthingPriorityLevel.BelowNormal),
LabelledValue.Create(Resources.SettingsView_Syncthing_ProcessPriority_Idle, Services.Config.SyncthingPriorityLevel.Idle),
};

this.Bind(s => s.AreAllFoldersNotified, (o, e) =>
{
if (this.updatingFolderSettings)
Expand Down
36 changes: 36 additions & 0 deletions src/SyncTrayzor/Properties/Resources.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 16 additions & 0 deletions src/SyncTrayzor/Properties/Resources.resx
Original file line number Diff line number Diff line change
Expand Up @@ -964,4 +964,20 @@ Please donate to my charity fundraising campaign.</value>
<value>Donate</value>
<comment>Shown in the top right of the menu bar, if the user hasn't yet donated</comment>
</data>
<data name="SettingsView_Logging_LogLevel" xml:space="preserve">
<value>Log Level:</value>
<comment>Label shown before SyncTrayzor logging level ComboBox</comment>
</data>
<data name="SettingsView_Logging_LogLevel_Debug" xml:space="preserve">
<value>Debug</value>
<comment>Name for the Debug log level (which provides lots of debug info)</comment>
</data>
<data name="SettingsView_Logging_LogLevel_Info" xml:space="preserve">
<value>Info (Default)</value>
<comment>Name for the Info log level</comment>
</data>
<data name="SettingsView_Logging_LogLevel_Trace" xml:space="preserve">
<value>Trace</value>
<comment>Name for the Trace log level (which provides the most debug info)</comment>
</data>
</root>
5 changes: 4 additions & 1 deletion src/SyncTrayzor/Services/Config/Configuration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ public string LatestNotifiedVersionRaw
public IconAnimationMode IconAnimationMode { get; set; }
public string OpenFolderCommand { get; set; }
public string ShowFileInFolderCommand { get; set; }
public LogLevel LogLevel { get; set; }

public Configuration()
{
Expand Down Expand Up @@ -113,6 +114,7 @@ public Configuration()
this.IconAnimationMode = IconAnimationMode.DataTransferring;
this.OpenFolderCommand = "explorer.exe \"{0}\"";
this.ShowFileInFolderCommand = "explorer.exe /select, \"{0}\"";
this.LogLevel = LogLevel.Info;
}

public Configuration(Configuration other)
Expand Down Expand Up @@ -151,6 +153,7 @@ public Configuration(Configuration other)
this.IconAnimationMode = other.IconAnimationMode;
this.OpenFolderCommand = other.OpenFolderCommand;
this.ShowFileInFolderCommand = other.ShowFileInFolderCommand;
this.LogLevel = other.LogLevel;
}

public override string ToString()
Expand All @@ -169,7 +172,7 @@ public override string ToString()
$"EnableConflictFileMonitoring={this.EnableConflictFileMonitoring} SyncthingDebugFacilities=[{String.Join(",", this.SyncthingDebugFacilities)}] "+
$"ConflictResolverDeletesToRecycleBin={this.ConflictResolverDeletesToRecycleBin} PauseDevicesOnMeteredNetworks={this.PauseDevicesOnMeteredNetworks} "+
$"HaveDonated={this.HaveDonated} IconAnimationMode={this.IconAnimationMode} OpenFolderCommand={this.OpenFolderCommand} ShowFileInFolderCommand={this.ShowFileInFolderCommand}" +
$">";
$"LogLevel={this.LogLevel}>";
}
}
}
15 changes: 15 additions & 0 deletions src/SyncTrayzor/Services/Config/LogLevel.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SyncTrayzor.Services.Config
{
public enum LogLevel
{
Info,
Debug,
Trace,
}
}
28 changes: 28 additions & 0 deletions src/SyncTrayzor/Services/ConfigurationApplicator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using System;
using System.Linq;
using SyncTrayzor.Services.Metering;
using System.Collections.Generic;

namespace SyncTrayzor.Services
{
Expand Down Expand Up @@ -116,6 +117,33 @@ private void ApplyNewConfiguration(Configuration configuration)

this.alertsManager.EnableConflictedFileAlerts = configuration.EnableConflictFileMonitoring;
this.alertsManager.EnableFailedTransferAlerts = configuration.EnableFailedTransferAlerts;

SetLogLevel(configuration);
}

private static readonly Dictionary<LogLevel, NLog.LogLevel> logLevelMapping = new Dictionary<Config.LogLevel, NLog.LogLevel>()
{
{ LogLevel.Info, NLog.LogLevel.Info },
{ LogLevel.Debug, NLog.LogLevel.Debug },
{ LogLevel.Trace, NLog.LogLevel.Trace },
};

private static void SetLogLevel(Configuration configuration)
{
var logLevel = logLevelMapping[configuration.LogLevel];
var rules = NLog.LogManager.Configuration.LoggingRules;
var logFileRule = rules.FirstOrDefault(rule => rule.Targets.Any(target => target.Name == "logfile"));
if (logFileRule != null)
{
foreach (var level in NLog.LogLevel.AllLoggingLevels)
{
if (level < logLevel)
logFileRule.DisableLoggingForLevel(level);
else
logFileRule.EnableLoggingForLevel(level);
}
NLog.LogManager.ReconfigExistingLoggers();
}
}

private void OnDataLoaded(object sender, EventArgs e)
Expand Down
1 change: 1 addition & 0 deletions src/SyncTrayzor/SyncTrayzor.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,7 @@
<Compile Include="Services\Config\EnvironmentalVariableCollection.cs" />
<Compile Include="Services\Config\FolderConfiguration.cs" />
<Compile Include="Services\Config\IconAnimationMode.cs" />
<Compile Include="Services\Config\LogLevel.cs" />
<Compile Include="Services\Config\SyncthingPriorityLevel.cs" />
<Compile Include="Services\Config\SyncTrayzorVariant.cs" />
<Compile Include="Services\Config\WindowPlacement.cs" />
Expand Down

0 comments on commit 48a6f3c

Please sign in to comment.