| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364 |
- using NLog;
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Runtime.CompilerServices;
- using System.Text;
- using System.Threading.Tasks;
- namespace SKMC.Api.Common.Logger
- {
- public class LogFactory
- {
- static LogFactory()
- {
- // NLog特殊处理,需要先注册Target再获取LOG实例,否则Target内部方法无法触发。
- NLog.LogManager.Setup()
- .SetupExtensions(ext =>
- {
- ext.RegisterTarget<LoggingProductionTarget>("LoggingProductionTarget");
- ext.RegisterTarget<LoggingParameterTarget>("LoggingParameterTarget");
- ext.RegisterTarget<LoggingProcessTarget>("LoggingProcessTarget");
- ext.RegisterTarget<LoggingActionTarget>("LoggingActionTarget");
- })
- .LoadConfigurationFromFile("nlog.config");
- }
- /// <summary>
- /// "Log4Net" 或 "NLog"
- /// </summary>
- private static readonly string LoggerType = "Log4Net";
- public static ILogger Get(Type type)
- {
- if (LoggerType == "Log4Net")
- {
- return new Log4netLogger(type);
- }
- else if (LoggerType == "NLog")
- {
- return new NLogLogger(type.FullName);
- }
- else
- {
- throw new NotSupportedException($"Logger type {LoggerType} is not supported.");
- }
- }
- public static ILogger Get([CallerFilePath] string callerFilePath = "")
- {
- var callerType = System.IO.Path.GetFileNameWithoutExtension(callerFilePath);
- if (LoggerType == "Log4Net")
- {
- return new Log4netLogger(callerType);
- }
- else if (LoggerType == "NLog")
- {
- return new NLogLogger(callerType);
- }
- else
- {
- throw new NotSupportedException($"Logger type {LoggerType} is not supported.");
- }
- }
- }
- }
|