[fxcop] Fixes for Wox.Plugin (2of3) - Moved logger interface to Wox.Plugin (#7464)

* Moved Logger/Log.cs from Wox.Infrastructure to Wox.Plugin

- Installed Logger dependency in Wox.Plugin: NLog.Extensions.Logging
- Moved file Log.cs from Wox.Infrastructure/Logger/ to Wox.Plugin/Logger
- Moved file Constant.cs from Wox.Infrastructure to Wox.Plugin: This file was moved since Log.cs depends on this class
    - Copied Wox.Infrastructure.Helper.NonNull to Wox.Plugin.Constant since Constant.cs depends on this method
- Replaced all "using Wox.Infrastructure.Logger" to "using Wox.Plugin.Logger" in all files as needed
- Replaced Wox.Infrastructure.Constant to Wox.Plugin.Constant in all files as needed

* Removed Nlog.Extensions.Logging from Wox.Infrastructure

* Added logging and suppressed general exceptions (CA1031: Do not catch general exception types)

* Resolved fxcop errors introduced by newly added Log.cs

- CA1307: Specify StringComparison for clarity
- CA2000: Dispose objects before losing scope
- CA1062: Validate arguments of public methods

* Replaced Wox.Infrastructure.Logger with Wox.Plugin.Logger
This commit is contained in:
Avneet Kaur
2020-10-23 13:06:22 -07:00
committed by GitHub
parent 6ae8d6749a
commit beecdc8d79
44 changed files with 105 additions and 44 deletions

View File

@@ -5,8 +5,8 @@
using System;
using System.Collections.Generic;
using System.Globalization;
using Wox.Infrastructure.Logger;
using Wox.Plugin;
using Wox.Plugin.Logger;
namespace Microsoft.Plugin.Calculator
{

View File

@@ -10,8 +10,8 @@ using System.Reflection;
using System.Windows;
using System.Windows.Input;
using Wox.Infrastructure;
using Wox.Infrastructure.Logger;
using Wox.Plugin;
using Wox.Plugin.Logger;
namespace Microsoft.Plugin.Folder
{

View File

@@ -10,8 +10,8 @@ using System.Reflection;
using System.Windows;
using System.Windows.Input;
using Wox.Infrastructure;
using Wox.Infrastructure.Logger;
using Wox.Plugin;
using Wox.Plugin.Logger;
namespace Microsoft.Plugin.Folder
{

View File

@@ -7,8 +7,8 @@ using System.Diagnostics;
using System.Globalization;
using System.Reflection;
using System.Text.RegularExpressions;
using Wox.Infrastructure.Logger;
using Wox.Plugin;
using Wox.Plugin.Logger;
namespace Microsoft.Plugin.Folder.Sources
{

View File

@@ -12,8 +12,8 @@ using System.Windows;
using System.Windows.Input;
using Microsoft.Plugin.Indexer.SearchHelper;
using Wox.Infrastructure;
using Wox.Infrastructure.Logger;
using Wox.Plugin;
using Wox.Plugin.Logger;
namespace Microsoft.Plugin.Indexer
{

View File

@@ -15,9 +15,9 @@ using Microsoft.Plugin.Indexer.DriveDetection;
using Microsoft.Plugin.Indexer.SearchHelper;
using Microsoft.PowerToys.Settings.UI.Library;
using Microsoft.Search.Interop;
using Wox.Infrastructure.Logger;
using Wox.Infrastructure.Storage;
using Wox.Plugin;
using Wox.Plugin.Logger;
namespace Microsoft.Plugin.Indexer
{

View File

@@ -7,7 +7,7 @@ using System.Diagnostics;
using System.IO;
using System.Runtime.CompilerServices;
using System.Security;
using Wox.Infrastructure.Logger;
using Wox.Plugin.Logger;
namespace Microsoft.Plugin.Program.Logger
{

View File

@@ -11,9 +11,9 @@ using System.Threading.Tasks;
using Microsoft.Plugin.Program.ProgramArgumentParser;
using Microsoft.Plugin.Program.Programs;
using Microsoft.Plugin.Program.Storage;
using Wox.Infrastructure.Logger;
using Wox.Infrastructure.Storage;
using Wox.Plugin;
using Wox.Plugin.Logger;
using Stopwatch = Wox.Infrastructure.Stopwatch;
namespace Microsoft.Plugin.Program

View File

@@ -6,7 +6,7 @@ using System;
using System.Collections.Generic;
using System.Security.Principal;
using Windows.Management.Deployment;
using Wox.Infrastructure.Logger;
using Wox.Plugin.Logger;
using Package = Windows.ApplicationModel.Package;
namespace Microsoft.Plugin.Program.Programs

View File

@@ -12,7 +12,7 @@ using System.Runtime.InteropServices.ComTypes;
using System.Xml.Linq;
using Microsoft.Plugin.Program.Logger;
using Microsoft.Plugin.Program.Win32;
using Wox.Infrastructure.Logger;
using Wox.Plugin.Logger;
namespace Microsoft.Plugin.Program.Programs
{

View File

@@ -20,8 +20,8 @@ using Microsoft.Plugin.Program.Logger;
using Microsoft.Plugin.Program.Win32;
using Wox.Infrastructure;
using Wox.Infrastructure.Image;
using Wox.Infrastructure.Logger;
using Wox.Plugin;
using Wox.Plugin.Logger;
using Wox.Plugin.SharedCommands;
using static Microsoft.Plugin.Program.Programs.UWP;

View File

@@ -18,8 +18,8 @@ using Microsoft.Plugin.Program.Logger;
using Microsoft.Win32;
using Wox.Infrastructure;
using Wox.Infrastructure.FileSystemHelper;
using Wox.Infrastructure.Logger;
using Wox.Plugin;
using Wox.Plugin.Logger;
namespace Microsoft.Plugin.Program.Programs
{

View File

@@ -9,8 +9,8 @@ using System.Collections.ObjectModel;
using System.Globalization;
using System.IO;
using System.Threading.Tasks;
using Wox.Infrastructure.Logger;
using Wox.Infrastructure.Storage;
using Wox.Plugin.Logger;
using Win32Program = Microsoft.Plugin.Program.Programs.Win32Program;
namespace Microsoft.Plugin.Program.Storage

View File

@@ -12,9 +12,9 @@ using System.Linq;
using System.Reflection;
using System.Windows.Input;
using Microsoft.PowerToys.Settings.UI.Library;
using Wox.Infrastructure.Logger;
using Wox.Infrastructure.Storage;
using Wox.Plugin;
using Wox.Plugin.Logger;
using Wox.Plugin.SharedCommands;
using Control = System.Windows.Controls.Control;

View File

@@ -8,9 +8,9 @@ using System.Diagnostics;
using System.IO;
using System.Text;
using Microsoft.Plugin.Uri.UriHelper;
using Wox.Infrastructure.Logger;
using Wox.Infrastructure.Storage;
using Wox.Plugin;
using Wox.Plugin.Logger;
namespace Microsoft.Plugin.Uri
{

View File

@@ -16,9 +16,9 @@ using Wox.Core.Plugin;
using Wox.Infrastructure;
using Wox.Infrastructure.Http;
using Wox.Infrastructure.Image;
using Wox.Infrastructure.Logger;
using Wox.Infrastructure.UserSettings;
using Wox.Plugin;
using Wox.Plugin.Logger;
using Stopwatch = Wox.Infrastructure.Stopwatch;
namespace PowerLauncher

View File

@@ -10,6 +10,7 @@ using System.Windows.Threading;
using NLog;
using Wox.Infrastructure;
using Wox.Infrastructure.Exception;
using Wox.Plugin;
namespace PowerLauncher.Helper
{

View File

@@ -15,7 +15,7 @@ using PowerLauncher.Helper;
using PowerLauncher.ViewModel;
using Wox.Infrastructure.UserSettings;
using KeyEventArgs = System.Windows.Input.KeyEventArgs;
using Log = Wox.Infrastructure.Logger.Log;
using Log = Wox.Plugin.Logger.Log;
using Screen = System.Windows.Forms.Screen;
namespace PowerLauncher

View File

@@ -15,7 +15,7 @@ using System.Windows.Navigation;
using PowerLauncher.Helper;
using Wox.Infrastructure;
using Wox.Infrastructure.Image;
using Wox.Infrastructure.Logger;
using Wox.Plugin.Logger;
namespace PowerLauncher
{

View File

@@ -11,9 +11,9 @@ using Microsoft.PowerToys.Settings.UI.Library.Utilities;
using PowerLauncher.Helper;
using Wox.Core.Plugin;
using Wox.Infrastructure.Hotkey;
using Wox.Infrastructure.Logger;
using Wox.Infrastructure.UserSettings;
using Wox.Plugin;
using Wox.Plugin.Logger;
using JsonException = System.Text.Json.JsonException;
namespace PowerLauncher

View File

@@ -9,8 +9,8 @@ using System.Windows.Media;
using PowerLauncher.Helper;
using Wox.Core.Plugin;
using Wox.Infrastructure.Image;
using Wox.Infrastructure.Logger;
using Wox.Plugin;
using Wox.Plugin.Logger;
namespace PowerLauncher.ViewModel
{

View File

@@ -8,8 +8,8 @@ using System.IO;
using System.Linq;
using System.Reflection;
using Newtonsoft.Json;
using Wox.Infrastructure.Logger;
using Wox.Plugin;
using Wox.Plugin.Logger;
namespace Wox.Core.Plugin
{

View File

@@ -8,8 +8,8 @@ using System.Reflection;
using System.Windows;
using ICSharpCode.SharpZipLib.Zip;
using Newtonsoft.Json;
using Wox.Infrastructure.Logger;
using Wox.Plugin;
using Wox.Plugin.Logger;
namespace Wox.Core.Plugin
{
@@ -41,7 +41,7 @@ namespace Wox.Core.Plugin
return;
}
string pluginFolderPath = Infrastructure.Constant.PluginsDirectory;
string pluginFolderPath = Constant.PluginsDirectory;
// Using Ordinal since this is part of a path
string newPluginName = plugin.Name

View File

@@ -11,10 +11,10 @@ using System.Linq;
using System.Reflection;
using System.Threading.Tasks;
using Wox.Infrastructure;
using Wox.Infrastructure.Logger;
using Wox.Infrastructure.Storage;
using Wox.Infrastructure.UserSettings;
using Wox.Plugin;
using Wox.Plugin.Logger;
namespace Wox.Core.Plugin
{

View File

@@ -10,6 +10,7 @@ using System.Runtime.Loader;
using Wox.Infrastructure;
using Wox.Infrastructure.UserSettings;
using Wox.Plugin;
using Wox.Plugin.Logger;
namespace Wox.Core.Plugin
{
@@ -46,7 +47,7 @@ namespace Wox.Core.Plugin
}
catch (Exception e)
{
Infrastructure.Logger.Log.Exception($"Couldn't load assembly for {metadata.Name}", e, MethodBase.GetCurrentMethod().DeclaringType);
Log.Exception($"Couldn't load assembly for {metadata.Name}", e, MethodBase.GetCurrentMethod().DeclaringType);
return;
}
@@ -58,7 +59,7 @@ namespace Wox.Core.Plugin
}
catch (InvalidOperationException e)
{
Infrastructure.Logger.Log.Exception($"Can't find class implement IPlugin for <{metadata.Name}>", e, MethodBase.GetCurrentMethod().DeclaringType);
Log.Exception($"Can't find class implement IPlugin for <{metadata.Name}>", e, MethodBase.GetCurrentMethod().DeclaringType);
return;
}
@@ -69,7 +70,7 @@ namespace Wox.Core.Plugin
}
catch (Exception e)
{
Infrastructure.Logger.Log.Exception($"Can't create instance for <{metadata.Name}>", e, MethodBase.GetCurrentMethod().DeclaringType);
Log.Exception($"Can't create instance for <{metadata.Name}>", e, MethodBase.GetCurrentMethod().DeclaringType);
return;
}
#endif

View File

@@ -8,7 +8,7 @@ using System.Linq;
using System.Reflection;
using System.Windows;
using System.Windows.Media;
using Wox.Infrastructure.Logger;
using Wox.Plugin.Logger;
namespace Wox.Core.Resource
{

View File

@@ -8,6 +8,7 @@ using System.Globalization;
using System.Linq;
using System.Text;
using Microsoft.Win32;
using Wox.Plugin;
namespace Wox.Infrastructure.Exception
{

View File

@@ -5,7 +5,7 @@
using System;
using System.IO;
using System.Security;
using Wox.Infrastructure.Logger;
using Wox.Plugin.Logger;
namespace Wox.Infrastructure.FileSystemHelper
{

View File

@@ -8,7 +8,7 @@ using System.IO;
using System.Reflection;
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;
using Wox.Infrastructure.Logger;
using Wox.Plugin.Logger;
namespace Wox.Infrastructure
{

View File

@@ -9,8 +9,8 @@ using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using JetBrains.Annotations;
using Wox.Infrastructure.Logger;
using Wox.Infrastructure.UserSettings;
using Wox.Plugin.Logger;
namespace Wox.Infrastructure.Http
{

View File

@@ -7,6 +7,7 @@ using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Media;
using Wox.Plugin;
namespace Wox.Infrastructure.Image
{

View File

@@ -11,9 +11,9 @@ using System.Reflection;
using System.Threading.Tasks;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using Wox.Infrastructure.Logger;
using Wox.Infrastructure.Storage;
using Wox.Plugin;
using Wox.Plugin.Logger;
namespace Wox.Infrastructure.Image
{

View File

@@ -5,7 +5,7 @@
using System;
using System.Collections.Generic;
using System.Reflection;
using Wox.Infrastructure.Logger;
using Wox.Plugin.Logger;
namespace Wox.Infrastructure
{

View File

@@ -8,7 +8,8 @@ using System.Reflection;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters;
using System.Runtime.Serialization.Formatters.Binary;
using Wox.Infrastructure.Logger;
using Wox.Plugin;
using Wox.Plugin.Logger;
namespace Wox.Infrastructure.Storage
{

View File

@@ -6,7 +6,7 @@ using System;
using System.Globalization;
using System.IO;
using Newtonsoft.Json;
using Wox.Infrastructure.Logger;
using Wox.Plugin.Logger;
namespace Wox.Infrastructure.Storage
{

View File

@@ -7,7 +7,7 @@ using System.Collections;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq;
using Wox.Infrastructure.Logger;
using Wox.Plugin.Logger;
namespace Wox.Infrastructure.Storage
{

View File

@@ -3,6 +3,7 @@
// See the LICENSE file in the project root for more information.
using System.IO;
using Wox.Plugin;
namespace Wox.Infrastructure.Storage
{

View File

@@ -3,6 +3,7 @@
// See the LICENSE file in the project root for more information.
using System.IO;
using Wox.Plugin;
namespace Wox.Infrastructure.Storage
{

View File

@@ -84,7 +84,7 @@ namespace Wox.Infrastructure.UserSettings
}
catch (ArgumentException e)
{
Logger.Log.Exception("Failed to load QuerySearchPrecisionString value from Settings file", e, GetType());
Wox.Plugin.Logger.Log.Exception("Failed to load QuerySearchPrecisionString value from Settings file", e, GetType());
QuerySearchPrecision = StringMatcher.SearchPrecisionScore.Regular;
StringMatcher.Instance.UserSettingSearchPrecision = StringMatcher.SearchPrecisionScore.Regular;

View File

@@ -1,4 +1,4 @@
<Project Sdk="Microsoft.NET.Sdk.WindowsDesktop">
<Project Sdk="Microsoft.NET.Sdk.WindowsDesktop">
<Import Project="..\..\..\Version.props" />
<PropertyGroup>
@@ -56,7 +56,6 @@
<ItemGroup>
<PackageReference Include="JetBrains.Annotations" Version="2020.1.0" />
<PackageReference Include="NLog.Extensions.Logging" Version="1.6.5" />
<PackageReference Include="NLog.Schema" Version="4.7.4" />
<PackageReference Include="System.Drawing.Common" Version="4.7.0" />
<PackageReference Include="System.Runtime" Version="4.3.1" />

View File

@@ -7,10 +7,25 @@ using System.Diagnostics;
using System.IO;
using System.Reflection;
namespace Wox.Infrastructure
namespace Wox.Plugin
{
public static class Constant
{
/// <summary>
/// http://www.yinwang.org/blog-cn/2015/11/21/programming-philosophy
/// </summary>
public static string NonNull(this string obj)
{
if (obj == null)
{
throw new NullReferenceException();
}
else
{
return obj;
}
}
public const string ExeFileName = "PowerLauncher";
public const string ModuleLocation = "Microsoft\\PowerToys\\PowerToys Run";
public const string Plugins = "Plugins";

View File

@@ -9,7 +9,7 @@ using NLog;
using NLog.Config;
using NLog.Targets;
namespace Wox.Infrastructure.Logger
namespace Wox.Plugin.Logger
{
public static class Log
{
@@ -28,7 +28,9 @@ namespace Wox.Infrastructure.Logger
var configuration = new LoggingConfiguration();
var target = new FileTarget();
configuration.AddTarget("file", target);
target.FileName = CurrentLogDirectory.Replace(@"\", "/") + "/${shortdate}.txt";
// Adding CurrentCulture since this is user facing
target.FileName = CurrentLogDirectory.Replace(@"\", "/", StringComparison.CurrentCulture) + "/${shortdate}.txt";
#if DEBUG
var rule = new LoggingRule("*", LogLevel.Debug, target);
#else
@@ -36,6 +38,7 @@ namespace Wox.Infrastructure.Logger
#endif
configuration.LoggingRules.Add(rule);
LogManager.Configuration = configuration;
target.Dispose();
}
private static void LogInternalException(string message, System.Exception e, Type fullClassName, [CallerMemberName] string methodName = "", [CallerFilePath] string sourceFilePath = "", [CallerLineNumber] int sourceLineNumber = 0)
@@ -66,26 +69,51 @@ namespace Wox.Infrastructure.Logger
public static void Info(string message, Type fullClassName, [CallerMemberName] string methodName = "", [CallerFilePath] string sourceFilePath = "", [CallerLineNumber] int sourceLineNumber = 0)
{
if (fullClassName == null)
{
throw new ArgumentNullException(nameof(fullClassName));
}
LogInternal(LogLevel.Info, message, fullClassName, methodName, sourceFilePath, sourceLineNumber);
}
public static void Debug(string message, Type fullClassName, [CallerMemberName] string methodName = "", [CallerFilePath] string sourceFilePath = "", [CallerLineNumber] int sourceLineNumber = 0)
{
if (fullClassName == null)
{
throw new ArgumentNullException(nameof(fullClassName));
}
LogInternal(LogLevel.Debug, message, fullClassName, methodName, sourceFilePath, sourceLineNumber);
}
public static void Warn(string message, Type fullClassName, [CallerMemberName] string methodName = "", [CallerFilePath] string sourceFilePath = "", [CallerLineNumber] int sourceLineNumber = 0)
{
if (fullClassName == null)
{
throw new ArgumentNullException(nameof(fullClassName));
}
LogInternal(LogLevel.Warn, message, fullClassName, methodName, sourceFilePath, sourceLineNumber);
}
public static void Error(string message, Type fullClassName, [CallerMemberName] string methodName = "", [CallerFilePath] string sourceFilePath = "", [CallerLineNumber] int sourceLineNumber = 0)
{
if (fullClassName == null)
{
throw new ArgumentNullException(nameof(fullClassName));
}
LogInternal(LogLevel.Error, message, fullClassName, methodName, sourceFilePath, sourceLineNumber);
}
public static void Exception(string message, System.Exception ex, Type fullClassName, [CallerMemberName] string methodName = "", [CallerFilePath] string sourceFilePath = "", [CallerLineNumber] int sourceLineNumber = 0)
{
if (fullClassName == null)
{
throw new ArgumentNullException(nameof(fullClassName));
}
LogInternalException(message, ex, fullClassName, methodName, sourceFilePath, sourceLineNumber);
}

View File

@@ -4,11 +4,14 @@
using System;
using System.IO;
using System.Reflection;
using Wox.Plugin.Logger;
namespace Wox.Plugin.SharedCommands
{
public static class FilesFolders
{
[System.Diagnostics.CodeAnalysis.SuppressMessage("Design", "CA1031:Do not catch general exception types", Justification = "Exception has been logged")]
public static void Copy(this string sourcePath, string targetPath)
{
// Get the subdirectories for the specified directory.
@@ -50,6 +53,8 @@ namespace Wox.Plugin.SharedCommands
catch (Exception e)
#pragma warning restore CS0168 // Variable is declared but never used
{
string error = $"Copying path {targetPath} has failed";
Log.Exception(error, e, MethodBase.GetCurrentMethod().DeclaringType);
#if DEBUG
throw e;
#else
@@ -59,6 +64,7 @@ namespace Wox.Plugin.SharedCommands
}
}
[System.Diagnostics.CodeAnalysis.SuppressMessage("Design", "CA1031:Do not catch general exception types", Justification = "Exception has been logged")]
public static bool VerifyBothFolderFilesEqual(this string fromPath, string toPath)
{
try
@@ -82,15 +88,18 @@ namespace Wox.Plugin.SharedCommands
catch (Exception e)
#pragma warning restore CS0168 // Variable is declared but never used
{
string error = $"Unable to verify folders and files between {fromPath} and {toPath}";
Log.Exception(error, e, MethodBase.GetCurrentMethod().DeclaringType);
#if DEBUG
throw e;
#else
System.Windows.MessageBox.Show(string.Format("Unable to verify folders and files between {0} and {1}", fromPath, toPath));
System.Windows.MessageBox.Show(string.Format(error));
return false;
#endif
}
}
[System.Diagnostics.CodeAnalysis.SuppressMessage("Design", "CA1031:Do not catch general exception types", Justification = "Exception has been logged")]
public static void RemoveFolder(this string path)
{
try
@@ -104,10 +113,12 @@ namespace Wox.Plugin.SharedCommands
catch (Exception e)
#pragma warning restore CS0168 // Variable is declared but never used
{
string error = $"Not able to delete folder {path}, please go to the location and manually delete it";
Log.Exception(error, e, MethodBase.GetCurrentMethod().DeclaringType);
#if DEBUG
throw e;
#else
System.Windows.MessageBox.Show(string.Format("Not able to delete folder {0}, please go to the location and manually delete it", path));
System.Windows.MessageBox.Show(string.Format(error));
#endif
}
}

View File

@@ -70,6 +70,7 @@
<PackageReference Include="ControlzEx" Version="4.3.2" />
<PackageReference Include="Mono.Cecil" Version="0.11.2" />
<PackageReference Include="Newtonsoft.Json" Version="12.0.3" />
<PackageReference Include="NLog.Extensions.Logging" Version="1.6.5" />
<PackageReference Include="PropertyChanged.Fody" Version="3.2.9">
<PrivateAssets>all</PrivateAssets>
</PackageReference>