Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
165 changes: 80 additions & 85 deletions src/ExchangeSharp/Utility/Logger.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,10 @@ The above copyright notice and this permission notice shall be included in all c
#region Imports

using System;
using System.Configuration;
using System.IO;
using System.Xml;
using NLog;
using NLog.Config;
using NLog.Targets;

#endregion Imports

Expand Down Expand Up @@ -103,7 +102,7 @@ public class LoggerEvent

/// <summary>
/// ExchangeSharp logger. Will never throw exceptions.
/// Currently the ExchangeSharp logger uses NLog internally, so make sure it is setup in your app.config file or nlog.config file.
/// Currently the ExchangeSharp logger uses NLog internally, so make sure it is setup in your app.config file or NLog.config file.
/// </summary>
public static class Logger
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I used Copilot (GPT5) to fix and modernize this constructor. @jjxtra, since you wrote this part originally, could you give it a close look to make sure it still does what you expect?

{
Expand All @@ -114,48 +113,54 @@ static Logger()
{
try
{
LogFactory factory = null;
if (
File.Exists(
ConfigurationManager
.OpenExeConfiguration(ConfigurationUserLevel.None)
.FilePath
)
)
// If configuration already provided by host, then keep it
var currentConfig = LogManager.Configuration;
if (currentConfig == null || currentConfig.AllTargets.Count == 0)
{
factory = LogManager.LoadConfiguration(
ConfigurationManager
.OpenExeConfiguration(ConfigurationUserLevel.None)
.FilePath
);
}
// Try load from conventional files in application base directory
var baseDir = AppContext.BaseDirectory;
string[] candidateFiles = new string[]
{
Path.Combine(baseDir, "NLog.config"),
Path.Combine(baseDir, "nlog.config")
};

if (factory == null || factory.Configuration.AllTargets.Count == 0)
{
if (File.Exists("nlog.config"))
string configPath = null;
foreach (var path in candidateFiles)
{
if (File.Exists(path))
{
configPath = path;
break;
}
}

if (!string.IsNullOrEmpty(configPath))
{
factory = LogManager.LoadConfiguration("nlog.config");
LogManager.Setup().LoadConfigurationFromFile(configPath);
}
else
{
using var resourceStream =
typeof(Logger).Assembly.GetManifestResourceStream(
"ExchangeSharp.nlog.config"
);
System.Diagnostics.Debug.Assert(
resourceStream != null,
nameof(resourceStream) + " != null"
);
using var sr = new StreamReader(resourceStream);
using var xr = XmlReader.Create(sr);
LogManager.Configuration = new XmlLoggingConfiguration(
xr,
Directory.GetCurrentDirectory()
);
factory = LogManager.LogFactory;
// Try load from embedded resource
using var resourceStream = typeof(Logger).Assembly.GetManifestResourceStream("ExchangeSharp.nlog.config");
if (resourceStream != null)
{
using var sr = new StreamReader(resourceStream);
var xml = sr.ReadToEnd();
LogManager.Setup().LoadConfigurationFromXml(xml);
}
else
{
// Last resort: simple in-code configuration to console
var cfg = new LoggingConfiguration();
var console = new ConsoleTarget("console");
cfg.AddRule(NLog.LogLevel.Info, NLog.LogLevel.Fatal, console);
LogManager.Configuration = cfg;
}
}
}
logger = factory.GetCurrentClassLogger();

logger = LogManager.GetCurrentClassLogger();
}
catch (Exception ex)
{
Expand All @@ -164,58 +169,48 @@ static Logger()
}
}

/// <summary>
/// Map IPBan log level to NLog log level
/// </summary>
/// <param name="logLevel">IPBan log level</param>
/// <returns>NLog log level</returns>
public static NLog.LogLevel GetNLogLevel(LogLevel logLevel)
{
/// <summary>
/// Map IPBan log level to NLog log level
/// </summary>
/// <param name="logLevel">IPBan log level</param>
/// <returns>NLog log level</returns>
public static NLog.LogLevel GetNLogLevel(LogLevel logLevel) => logLevel switch
{
LogLevel.Critical => NLog.LogLevel.Fatal,
LogLevel.Debug => NLog.LogLevel.Debug,
LogLevel.Error => NLog.LogLevel.Error,
LogLevel.Information => NLog.LogLevel.Info,
LogLevel.Trace => NLog.LogLevel.Trace,
LogLevel.Warning => NLog.LogLevel.Warn,
_ => NLog.LogLevel.Off,
};

/*
/// <summary>
/// Map Microsoft log level to NLog log level
/// </summary>
/// <param name="logLevel">Microsoft log level</param>
/// <returns>NLog log level</returns>
public static NLog.LogLevel GetNLogLevel(Microsoft.Extensions.Logging.LogLevel logLevel)
{
switch (logLevel)
{
case LogLevel.Critical:
return NLog.LogLevel.Fatal;
case LogLevel.Debug:
return NLog.LogLevel.Debug;
case LogLevel.Error:
return NLog.LogLevel.Error;
case LogLevel.Information:
return NLog.LogLevel.Info;
case LogLevel.Trace:
return NLog.LogLevel.Trace;
case LogLevel.Warning:
return NLog.LogLevel.Warn;
default:
return NLog.LogLevel.Off;
case Microsoft.Extensions.Logging.LogLevel.Critical: return NLog.LogLevel.Fatal;
case Microsoft.Extensions.Logging.LogLevel.Debug: return NLog.LogLevel.Debug;
case Microsoft.Extensions.Logging.LogLevel.Error: return NLog.LogLevel.Error;
case Microsoft.Extensions.Logging.LogLevel.Information: return NLog.LogLevel.Info;
case Microsoft.Extensions.Logging.LogLevel.Trace: return NLog.LogLevel.Trace;
case Microsoft.Extensions.Logging.LogLevel.Warning: return NLog.LogLevel.Warn;
default: return NLog.LogLevel.Off;
}
}

/*
/// <summary>
/// Map Microsoft log level to NLog log level
/// </summary>
/// <param name="logLevel">Microsoft log level</param>
/// <returns>NLog log level</returns>
public static NLog.LogLevel GetNLogLevel(Microsoft.Extensions.Logging.LogLevel logLevel)
{
switch (logLevel)
{
case Microsoft.Extensions.Logging.LogLevel.Critical: return NLog.LogLevel.Fatal;
case Microsoft.Extensions.Logging.LogLevel.Debug: return NLog.LogLevel.Debug;
case Microsoft.Extensions.Logging.LogLevel.Error: return NLog.LogLevel.Error;
case Microsoft.Extensions.Logging.LogLevel.Information: return NLog.LogLevel.Info;
case Microsoft.Extensions.Logging.LogLevel.Trace: return NLog.LogLevel.Trace;
case Microsoft.Extensions.Logging.LogLevel.Warning: return NLog.LogLevel.Warn;
default: return NLog.LogLevel.Off;
}
}
*/
}
*/

/// <summary>
/// Log an error
/// </summary>
/// <param name="ex">Error</param>
public static void Error(Exception ex)
/// <summary>
/// Log an error
/// </summary>
/// <param name="ex">Error</param>
public static void Error(Exception ex)
{
Write(LogLevel.Error, "Exception: " + ex.ToString());
}
Expand Down
Loading