[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

@@ -1,52 +0,0 @@
// Copyright (c) Microsoft Corporation
// The Microsoft Corporation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using System;
using System.Diagnostics;
using System.IO;
using System.Reflection;
namespace Wox.Infrastructure
{
public static class Constant
{
public const string ExeFileName = "PowerLauncher";
public const string ModuleLocation = "Microsoft\\PowerToys\\PowerToys Run";
public const string Plugins = "Plugins";
public const string PortableFolderName = "UserData";
private static readonly Assembly Assembly = Assembly.GetExecutingAssembly();
public static readonly string ProgramDirectory = Directory.GetParent(Assembly.Location.NonNull()).ToString();
public static readonly string ExecutablePath = Path.Combine(ProgramDirectory, ExeFileName + ".exe");
public static bool IsPortableMode { get; set; }
public static string PortableDataPath { get; set; } = Path.Combine(ProgramDirectory, PortableFolderName);
public static string DetermineDataDirectory()
{
if (Directory.Exists(PortableDataPath))
{
IsPortableMode = true;
return PortableDataPath;
}
else
{
return Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), ModuleLocation);
}
}
public static readonly string DataDirectory = DetermineDataDirectory();
public static readonly string PluginsDirectory = Path.Combine(DataDirectory, Plugins);
public static readonly string PreinstalledDirectory = Path.Combine(ProgramDirectory, Plugins);
public const string Issue = "https://aka.ms/powerToysReportBug";
public static readonly string Version = FileVersionInfo.GetVersionInfo(Assembly.Location.NonNull()).ProductVersion;
public static readonly int ThumbnailSize = 64;
public static readonly string DefaultIcon = Path.Combine(ProgramDirectory, "Images", "app.dark.png");
public static readonly string ErrorIcon = Path.Combine(ProgramDirectory, "Images", "app_error.dark.png");
public static readonly string LightThemedDefaultIcon = Path.Combine(ProgramDirectory, "Images", "app.light.png");
public static readonly string LightThemedErrorIcon = Path.Combine(ProgramDirectory, "Images", "app_error.light.png");
}
}

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

@@ -1,116 +0,0 @@
// Copyright (c) Microsoft Corporation
// The Microsoft Corporation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using System;
using System.IO;
using System.Runtime.CompilerServices;
using NLog;
using NLog.Config;
using NLog.Targets;
namespace Wox.Infrastructure.Logger
{
public static class Log
{
public const string DirectoryName = "Logs";
public static string CurrentLogDirectory { get; }
static Log()
{
CurrentLogDirectory = Path.Combine(Constant.DataDirectory, DirectoryName, Constant.Version);
if (!Directory.Exists(CurrentLogDirectory))
{
Directory.CreateDirectory(CurrentLogDirectory);
}
var configuration = new LoggingConfiguration();
var target = new FileTarget();
configuration.AddTarget("file", target);
target.FileName = CurrentLogDirectory.Replace(@"\", "/") + "/${shortdate}.txt";
#if DEBUG
var rule = new LoggingRule("*", LogLevel.Debug, target);
#else
var rule = new LoggingRule("*", LogLevel.Info, target);
#endif
configuration.LoggingRules.Add(rule);
LogManager.Configuration = configuration;
}
private static void LogInternalException(string message, System.Exception e, Type fullClassName, [CallerMemberName] string methodName = "", [CallerFilePath] string sourceFilePath = "", [CallerLineNumber] int sourceLineNumber = 0)
{
var logger = GetLogger(fullClassName.FullName, methodName);
LogInternal(LogLevel.Error, message, fullClassName, logger, methodName, sourceFilePath, sourceLineNumber);
logger.Error("-------------------------- Begin exception --------------------------");
logger.Error($"\n\tMessage:\n\t {message}");
do
{
logger.Error(
$"\n\tException full name:\n\t <{e.GetType().FullName}>" +
$"\n\tException message:\n\t <{e.Message}>" +
$"\n\tException stack trace:\n\t <{e.StackTrace}>" +
$"\n\tException source:\n\t <{e.Source}>" +
$"\n\tException target site:\n\t <{e.TargetSite}>" +
$"\n\tException HResult:\n\t <{e.HResult}>");
e = e.InnerException;
}
while (e != null);
logger.Error("-------------------------- End exception --------------------------");
}
public static void Info(string message, Type fullClassName, [CallerMemberName] string methodName = "", [CallerFilePath] string sourceFilePath = "", [CallerLineNumber] int sourceLineNumber = 0)
{
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)
{
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)
{
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)
{
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)
{
LogInternalException(message, ex, fullClassName, methodName, sourceFilePath, sourceLineNumber);
}
private static void LogInternal(LogLevel level, string message, Type fullClassName, [CallerMemberName] string methodName = "", [CallerFilePath] string sourceFilePath = "", [CallerLineNumber] int sourceLineNumber = 0)
{
var logger = GetLogger(fullClassName.FullName, methodName);
LogInternal(level, message, fullClassName, logger, methodName, sourceFilePath, sourceLineNumber);
}
private static void LogInternal(LogLevel level, string message, Type fullClassName, NLog.Logger logger, [CallerMemberName] string methodName = "", [CallerFilePath] string sourceFilePath = "", [CallerLineNumber] int sourceLineNumber = 0)
{
// System.Diagnostics.Debug.WriteLine($" {level.Name} | {message}");
var msg = $"\n\tMessage: {message}" +
$"\n\tArea: {fullClassName}.{methodName}" +
$"\n\tSource Path: {sourceFilePath}::{sourceLineNumber}\n";
logger.Log(level, msg);
}
private static NLog.Logger GetLogger(string fullClassName, string methodName)
{
var classNameWithMethod = $"{fullClassName}.{methodName}";
return LogManager.GetLogger(classNameWithMethod);
}
}
}

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" />