Skip to content

Commit

Permalink
Remove popup console logger and use log4net.
Browse files Browse the repository at this point in the history
Add verbose logging setting.
Logs to rolling file by default, 1MB, 2 files, 2MB total.
  • Loading branch information
halsafar committed Feb 19, 2022
1 parent e685780 commit 16c2b40
Show file tree
Hide file tree
Showing 8 changed files with 68 additions and 99 deletions.
27 changes: 27 additions & 0 deletions TaskBarAppIdAdjuster/App.config
Original file line number Diff line number Diff line change
@@ -1,6 +1,33 @@
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" />
</configSections>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
</startup>
<log4net>
<appender name="ConsoleAppender" type="log4net.Appender.ConsoleAppender">
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date %level %message%newline" />
</layout>
</appender>
<appender name="LogFileAppender" type="log4net.Appender.RollingFileAppender">
<param name="File" value="TaskBarAppIdAdjuster.log"/>
<lockingModel type="log4net.Appender.FileAppender+MinimalLock" />
<appendToFile value="true" />
<rollingStyle value="Size" />
<maxSizeRollBackups value="2" />
<maximumFileSize value="1MB" />
<staticLogFileName value="true" />
<layout type="log4net.Layout.PatternLayout">
<param name="ConversionPattern" value="%d [%t] %-5p %c %m%n"/>
</layout>
</appender>
<root>
<level value="INFO" />
<appender-ref ref="LogFileAppender" />
<appender-ref ref="ConsoleAppender" />
</root>
</log4net>
</configuration>
72 changes: 0 additions & 72 deletions TaskBarAppIdAdjuster/NativeConsole.cs

This file was deleted.

17 changes: 4 additions & 13 deletions TaskBarAppIdAdjuster/Program.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Diagnostics;
using System.Windows.Forms;

namespace TaskBarAppIdAdjuster
Expand All @@ -11,6 +12,8 @@ public class TaskBarAppIdApplicationContext : ApplicationContext

private Form1 _form = null;

private Process _logViewerProcess = null;

/// <summary>
/// Constructor
/// </summary>
Expand Down Expand Up @@ -98,19 +101,7 @@ void OnToggleAutoStart(object sender, EventArgs e)
void OnOpenLog(object sender, EventArgs e)
{
MenuItem item = sender as MenuItem;
if (item.Text == "Open Log")
{
NativeConsole.OpenConsole();

item.Text = "Close Log";
}
else
{
NativeConsole.CloseConsole();

item.Text = "Open Log";
}

_logViewerProcess = Process.Start(@"TaskBarAppIdAdjuster.log");
}

/// <summary>
Expand Down
1 change: 1 addition & 0 deletions TaskBarAppIdAdjuster/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
[assembly: AssemblyCopyright("Copyright © 2022")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]
[assembly: log4net.Config.XmlConfigurator(Watch = true)]

// Setting ComVisible to false makes the types in this assembly not visible
// to COM components. If you need to access a type in this assembly from
Expand Down
3 changes: 3 additions & 0 deletions TaskBarAppIdAdjuster/Settings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,9 @@ public class Settings
[DataMember]
public bool AutoStart = false;

[DataMember]
public bool verboseLogging = false;

/// <summary>
/// Constructor.
/// </summary>
Expand Down
6 changes: 5 additions & 1 deletion TaskBarAppIdAdjuster/TaskBarAppIdAdjuster.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,9 @@
<ApplicationIcon>icons8-taskbar-48.ico</ApplicationIcon>
</PropertyGroup>
<ItemGroup>
<Reference Include="log4net, Version=2.0.14.0, Culture=neutral, PublicKeyToken=669e0ddf0bb1aa2a, processorArchitecture=MSIL">
<HintPath>packages\log4net.2.0.14\lib\net45\log4net.dll</HintPath>
</Reference>
<Reference Include="Microsoft.WindowsAPICodePack, Version=1.1.0.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>packages\WindowsAPICodePack-Core.1.1.1\lib\Microsoft.WindowsAPICodePack.dll</HintPath>
</Reference>
Expand All @@ -60,8 +63,10 @@
</Reference>
<Reference Include="PresentationFramework" />
<Reference Include="System" />
<Reference Include="System.Configuration" />
<Reference Include="System.Core" />
<Reference Include="System.Runtime.Serialization" />
<Reference Include="System.Web" />
<Reference Include="System.Xml.Linq" />
<Reference Include="System.Data.DataSetExtensions" />
<Reference Include="Microsoft.CSharp" />
Expand All @@ -79,7 +84,6 @@
<Compile Include="Form1.Designer.cs">
<DependentUpon>Form1.cs</DependentUpon>
</Compile>
<Compile Include="NativeConsole.cs" />
<Compile Include="NativeWindowHelpers.cs" />
<Compile Include="Program.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
Expand Down
40 changes: 27 additions & 13 deletions TaskBarAppIdAdjuster/TaskBarService.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using Microsoft.WindowsAPICodePack.Taskbar;
using log4net;
using log4net.Core;
using Microsoft.WindowsAPICodePack.Taskbar;
using System;
using System.Collections.Generic;
using System.Diagnostics;
Expand Down Expand Up @@ -29,6 +31,8 @@ public Settings Settings
get { return _settings; }
}

private static readonly ILog log = LogManager.GetLogger(typeof(TaskBarAppIdApplicationContext));

/// <summary>
/// Constructor
/// </summary>
Expand All @@ -48,22 +52,32 @@ public void RefreshConfig()
{
_settings = Settings.Load();
_settingLastWriteTime = File.GetLastWriteTimeUtc(Settings.ConfigPath());

// adjust log level
if (_settings.verboseLogging)
{
foreach (var r in LogManager.GetAllRepositories())
{
((log4net.Repository.Hierarchy.Hierarchy)r).Root.Level = Level.Debug;
((log4net.Repository.Hierarchy.Hierarchy)r).RaiseConfigurationChanged(EventArgs.Empty);
}
}
}
else
{
DateTime curWriteTime = File.GetLastWriteTimeUtc(Settings.ConfigPath());
long sub = (long)(curWriteTime - _settingLastWriteTime).TotalMilliseconds;
if (sub > 0)
{
Console.WriteLine("Detected change to settings file...");
log.Info("Detected change to settings file...");
_settings = Settings.Load();
_settingLastWriteTime = curWriteTime;
}
}
}
catch (Exception)
{
Console.WriteLine("Failed to load config file, no actions will be taken until the file is fixed...");
log.Error("Failed to load config file, no actions will be taken until the file is fixed...");
_settings = null;
}
}
Expand All @@ -75,7 +89,7 @@ public void Start()
{
if (this._running)
{
Console.WriteLine("Already running.");
log.Warn("Already running.");
return;
}

Expand All @@ -94,7 +108,7 @@ public void Stop()
{
if (_thread == null || !_thread.IsAlive)
{
Console.WriteLine("Thread is already stopped or was never started.");
log.Warn("Thread is already stopped or was never started.");
return;
}

Expand All @@ -116,7 +130,7 @@ private void MainLoop()
};

HandleProcesses();
Console.WriteLine("Watching processes, press Ctrl+C to quit.");
log.Info("Starting to watch for processes to match...");
while (this._running)
{
RefreshConfig();
Expand All @@ -128,11 +142,11 @@ private void MainLoop()
}
catch (ThreadInterruptedException)
{
Console.WriteLine("Sleep interrupted!");
log.Debug("Sleep interrupted!");
}
}

Console.WriteLine("Goodbye");
log.Info("Goodbye");
}

/// <summary>
Expand All @@ -147,7 +161,7 @@ private void HandleProcesses()

foreach (TaskSetting taskSetting in _settings.ApplicationsToRandomize)
{
Console.WriteLine("Searching for any process matching the name: {0}", taskSetting.Name);
log.Debug($"Searching for any process matching the name: {taskSetting.Name}");

List<Process> processes = new List<Process>();

Expand All @@ -174,7 +188,7 @@ private void HandleProcesses()
// Assume if main process has no window then the rest do not?
if (process.MainWindowTitle.Length <= 0)
{
Console.WriteLine("Process matched but has no Window Title, can safely assume no Window, not randomizing: " + process.ProcessName);
log.Debug($"Process matched but has no Window Title, can safely assume no Window, not randomizing: {process.ProcessName}");
continue;
}

Expand All @@ -193,21 +207,21 @@ private void HandleProcesses()

if (_adjustedProcesses.Contains(identifier))
{
Console.WriteLine("Process {0}-{1} has already been adjusted.", taskSetting.Name, process.Id);
log.Debug($"Process {taskSetting.Name}-{process.Id} has already been adjusted.");
continue;
}

// Perform desired action on process
if (taskSetting.Action == TaskAction.Ungroup)
{
Guid g = Guid.NewGuid();
Console.WriteLine("Setting process {0} to random group {1}", process.ProcessName, g.ToString());
log.Debug($"Setting process {process.ProcessName} to random group {g.ToString()}");
TaskbarManager.Instance.SetApplicationIdForSpecificWindow(handle, g.ToString());
}
else if (taskSetting.Action == TaskAction.Group)
{
String groupId = "tbAdjusterGroup_" + taskSetting.Name;
Console.WriteLine("Setting process {0} to specific group {1}", process.ProcessName, groupId);
log.Debug($"Setting process {process.ProcessName} to specific group {groupId}");
TaskbarManager.Instance.SetApplicationIdForSpecificWindow(handle, groupId);
}

Expand Down
1 change: 1 addition & 0 deletions TaskBarAppIdAdjuster/packages.config
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="log4net" version="2.0.14" targetFramework="net45" />
<package id="Newtonsoft.Json" version="11.0.2" targetFramework="net45" />
<package id="WindowsAPICodePack-Core" version="1.1.1" targetFramework="net45" />
<package id="WindowsAPICodePack-Shell" version="1.1.1" targetFramework="net45" />
Expand Down

0 comments on commit 16c2b40

Please sign in to comment.