mirror of
https://github.com/microsoft/PowerToys.git
synced 2026-04-08 04:07:40 +02:00
Fix logger name and move logger to %APPDATA%
This commit is contained in:
@@ -1,14 +1,43 @@
|
|||||||
using NLog;
|
using System.Diagnostics;
|
||||||
using Wox.Infrastructure.Exception;
|
using System.IO;
|
||||||
|
using NLog;
|
||||||
|
using NLog.Config;
|
||||||
|
using NLog.Targets;
|
||||||
|
|
||||||
namespace Wox.Infrastructure.Logger
|
namespace Wox.Infrastructure.Logger
|
||||||
{
|
{
|
||||||
public class Log
|
public static class Log
|
||||||
{
|
{
|
||||||
private static NLog.Logger logger = LogManager.GetCurrentClassLogger();
|
static Log()
|
||||||
|
{
|
||||||
|
var directoryName = "Logs";
|
||||||
|
var path = Path.Combine(Wox.DataPath, directoryName);
|
||||||
|
if (!Directory.Exists(path))
|
||||||
|
{
|
||||||
|
Directory.CreateDirectory(path);
|
||||||
|
}
|
||||||
|
|
||||||
|
var configuration = new LoggingConfiguration();
|
||||||
|
var target = new FileTarget();
|
||||||
|
configuration.AddTarget("file", target);
|
||||||
|
target.FileName = "${specialfolder:folder=ApplicationData}/" + Wox.Name + "/" + directoryName + "/${shortdate}.log";
|
||||||
|
var rule = new LoggingRule("*", LogLevel.Info, target);
|
||||||
|
configuration.LoggingRules.Add(rule);
|
||||||
|
LogManager.Configuration = configuration;
|
||||||
|
}
|
||||||
|
private static string CallerType()
|
||||||
|
{
|
||||||
|
var stackTrace = new StackTrace();
|
||||||
|
var stackFrames = stackTrace.GetFrames().RequireNonNull();
|
||||||
|
var callingFrame = stackFrames[2];
|
||||||
|
var method = callingFrame.GetMethod();
|
||||||
|
var type = $"{method.DeclaringType.RequireNonNull().FullName}.{method.Name}";
|
||||||
|
return type;
|
||||||
|
}
|
||||||
public static void Error(System.Exception e)
|
public static void Error(System.Exception e)
|
||||||
{
|
{
|
||||||
|
var type = CallerType();
|
||||||
|
var logger = LogManager.GetLogger(type);
|
||||||
#if DEBUG
|
#if DEBUG
|
||||||
throw e;
|
throw e;
|
||||||
#else
|
#else
|
||||||
@@ -23,24 +52,32 @@ namespace Wox.Infrastructure.Logger
|
|||||||
|
|
||||||
public static void Debug(string msg)
|
public static void Debug(string msg)
|
||||||
{
|
{
|
||||||
|
var type = CallerType();
|
||||||
|
var logger = LogManager.GetLogger(type);
|
||||||
System.Diagnostics.Debug.WriteLine($"DEBUG: {msg}");
|
System.Diagnostics.Debug.WriteLine($"DEBUG: {msg}");
|
||||||
logger.Debug(msg);
|
logger.Debug(msg);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void Info(string msg)
|
public static void Info(string msg)
|
||||||
{
|
{
|
||||||
|
var type = CallerType();
|
||||||
|
var logger = LogManager.GetLogger(type);
|
||||||
System.Diagnostics.Debug.WriteLine($"INFO: {msg}");
|
System.Diagnostics.Debug.WriteLine($"INFO: {msg}");
|
||||||
logger.Info(msg);
|
logger.Info(msg);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void Warn(string msg)
|
public static void Warn(string msg)
|
||||||
{
|
{
|
||||||
|
var type = CallerType();
|
||||||
|
var logger = LogManager.GetLogger(type);
|
||||||
System.Diagnostics.Debug.WriteLine($"WARN: {msg}");
|
System.Diagnostics.Debug.WriteLine($"WARN: {msg}");
|
||||||
logger.Warn(msg);
|
logger.Warn(msg);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void Fatal(System.Exception e)
|
public static void Fatal(System.Exception e)
|
||||||
{
|
{
|
||||||
|
var type = CallerType();
|
||||||
|
var logger = LogManager.GetLogger(type);
|
||||||
#if DEBUG
|
#if DEBUG
|
||||||
throw e;
|
throw e;
|
||||||
#else
|
#else
|
||||||
|
|||||||
@@ -1,23 +0,0 @@
|
|||||||
<?xml version="1.0" encoding="utf-8" ?>
|
|
||||||
<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd"
|
|
||||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
|
|
||||||
|
|
||||||
<!--
|
|
||||||
See https://github.com/nlog/nlog/wiki/Configuration-file
|
|
||||||
for information on customizing logging rules and outputs.
|
|
||||||
You can use http://www.legitlog.com/ to visualize those logs
|
|
||||||
-->
|
|
||||||
<!-- log level
|
|
||||||
Trace - very detailed logs, which may include high-volume information such as protocol payloads. This log level is typically only enabled during development
|
|
||||||
Debug - debugging information, less detailed than trace, typically not enabled in production environment.
|
|
||||||
Info - information messages, which are normally enabled in production environment
|
|
||||||
Warn - warning messages, typically for non-critical issues, which can be recovered or which are temporary failures
|
|
||||||
Error - error messages
|
|
||||||
Fatal - very serious errors-->
|
|
||||||
<targets>
|
|
||||||
<target xsi:type="File" name="file" fileName="${basedir}/Logs/${shortdate}.log"/>
|
|
||||||
</targets>
|
|
||||||
<rules>
|
|
||||||
<logger name="*" minlevel="Info" writeTo="file" />
|
|
||||||
</rules>
|
|
||||||
</nlog>
|
|
||||||
@@ -2,9 +2,12 @@
|
|||||||
|
|
||||||
namespace Wox.Infrastructure
|
namespace Wox.Infrastructure
|
||||||
{
|
{
|
||||||
static class SyntaxSuger<T>
|
static class Helper
|
||||||
{
|
{
|
||||||
public static T RequireNonNull(T obj)
|
/// <summary>
|
||||||
|
/// http://www.yinwang.org/blog-cn/2015/11/21/programming-philosophy
|
||||||
|
/// </summary>
|
||||||
|
public static T RequireNonNull<T>(this T obj)
|
||||||
{
|
{
|
||||||
if (obj == null)
|
if (obj == null)
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -98,11 +98,6 @@
|
|||||||
<Name>Wox.Plugin</Name>
|
<Name>Wox.Plugin</Name>
|
||||||
</ProjectReference>
|
</ProjectReference>
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
<ItemGroup>
|
|
||||||
<Content Include="NLog.config">
|
|
||||||
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
|
|
||||||
</Content>
|
|
||||||
</ItemGroup>
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<None Include="packages.config" />
|
<None Include="packages.config" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|||||||
Reference in New Issue
Block a user