Fix logger name and move logger to %APPDATA%

This commit is contained in:
bao-qian
2016-04-27 22:51:31 +01:00
parent dc3b01dc15
commit 5ab33e831d
4 changed files with 46 additions and 34 deletions

View File

@@ -1,14 +1,43 @@
using NLog;
using Wox.Infrastructure.Exception;
using System.Diagnostics;
using System.IO;
using NLog;
using NLog.Config;
using NLog.Targets;
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)
{
var type = CallerType();
var logger = LogManager.GetLogger(type);
#if DEBUG
throw e;
#else
@@ -23,24 +52,32 @@ namespace Wox.Infrastructure.Logger
public static void Debug(string msg)
{
var type = CallerType();
var logger = LogManager.GetLogger(type);
System.Diagnostics.Debug.WriteLine($"DEBUG: {msg}");
logger.Debug(msg);
}
public static void Info(string msg)
{
var type = CallerType();
var logger = LogManager.GetLogger(type);
System.Diagnostics.Debug.WriteLine($"INFO: {msg}");
logger.Info(msg);
}
public static void Warn(string msg)
{
var type = CallerType();
var logger = LogManager.GetLogger(type);
System.Diagnostics.Debug.WriteLine($"WARN: {msg}");
logger.Warn(msg);
}
public static void Fatal(System.Exception e)
{
var type = CallerType();
var logger = LogManager.GetLogger(type);
#if DEBUG
throw e;
#else

View File

@@ -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>

View File

@@ -2,9 +2,12 @@
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)
{

View File

@@ -98,11 +98,6 @@
<Name>Wox.Plugin</Name>
</ProjectReference>
</ItemGroup>
<ItemGroup>
<Content Include="NLog.config">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Content>
</ItemGroup>
<ItemGroup>
<None Include="packages.config" />
</ItemGroup>