Skip to content

Commit

Permalink
添加日志记录器
Browse files Browse the repository at this point in the history
  • Loading branch information
ape-byte committed Aug 17, 2023
1 parent 844913b commit 0b8efda
Show file tree
Hide file tree
Showing 2 changed files with 105 additions and 0 deletions.
57 changes: 57 additions & 0 deletions BarrageGrab/Logger.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using NLog;

namespace BarrageGrab
{
public static class Logger
{
private static NLog.Config.ISetupBuilder builder = LogManager.Setup().LoadConfigurationFromFile("nlog.config");

private static readonly NLog.Logger logger = builder.GetLogger("*");

// 记录日志方法
public static void LogTrace(string message)
{
logger.Trace(message);
}

public static void LogDebug(string message)
{
logger.Debug(message);
}

public static void LogInfo(string message)
{
logger.Info(message);
}

public static void LogWarn(string message)
{
logger.Warn(message);
}

public static void LogError(string message)
{
logger.Error(message);
}

public static void LogError(Exception ex, string message)
{
logger.Error(ex, message);
}

public static void LogFatal(string message)
{
logger.Fatal(message);
}

public static void LogFatal(Exception ex, string message)
{
logger.Fatal(ex, message);
}
}
}
48 changes: 48 additions & 0 deletions BarrageGrab/nlog.config
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<!-- nlog 配置,支持文件和控制台日志 -->
<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<extensions>
<add assembly="NLog.Web.AspNetCore"/>
</extensions>
<targets>

<!--调试日志-->
<target name="debug"
xsi:type="Console"
layout="[${time} ${message} [${event-context:item=ip}] [${event-properties:ip}] ${newline}"
/>

<!--通用文件日志-->
<target
name="file"
xsi:type="File"
fileName="${basedir}/logs/error/${shortdate}.log"
archiveEvery="Day"
archiveNumbering="DateAndSequence"
maxArchiveFiles="30"
layout="${longdate}|${level:uppercase=true}| ${message} ${exception:format=tostring} ${newline}${newline}"/>

<!-- 控制台日志 -->
<target
name="console_info"
xsi:type="Console"
layout="${time}|${level:uppercase=true}| ${message} ${newline}"/>

<!-- 控制台日志-错误 颜色红色 -->
<target
name="console_error"
xsi:type="Console"
layout="${shortdate}|${level:uppercase=true}| ${message} ${newline}"
error="true"
/>

</targets>
<rules>
<logger name="*" minlevel="Error" writeTo="file,console_error" />
<logger name="*" level="Info" writeTo="console_info" />
<logger name="debug" minlevel="Trace" writeTo="debug" />
<!-- debug,以及 info 在控制台输出 -->
</rules>
</nlog>


0 comments on commit 0b8efda

Please sign in to comment.