Reputation: 45
I ran into a issue because LogManager is global, so my two projects were overriding each other's logging config. NLog log calls in the first project would work fine, but then when calling into the second project, all future log calls would be logged with the second projects config.
Example:
I found Isolated logfactory which solved my issue, now the loggers has the correct config. However I cannot figure out how to use my custom NLog class with this method of making LogFactories.
This works great (but doesn't let me use my custom class with methods):
private static Logger logger = MyLogger.MyLogManager.Instance.GetCurrentClassLogger();
This:
private static MyLogger logger = (MyLogger)MyLogger.MyLogManager.Instance.GetCurrentClassLogger();
Throws:
System.TypeInitializationException: 'The type initializer for 'MyProject.MyClass.MyMethod' threw an exception.'
InvalidCastException: Unable to cast object of type 'NLog.Logger' to type 'MyProject.NLogConfigFolder.MyLogger'.
I have tried to cast Logger to MyLogger but have not been able to do so successfully.
Here is my setup for the isolated LogFactory:
public class MyLogger : Logger
{
public class MyLogManager
{
public static LogFactory Instance { get { return _instance.Value; } }
private static Lazy<LogFactory> _instance = new Lazy<LogFactory>(BuildLogFactory);
private static LogFactory BuildLogFactory()
{
string configFilePath = "path/to/my/config"
LogFactory logFactory = new LogFactory();
logFactory.Configuration = new XmlLoggingConfiguration(configFilePath, true, logFactory);
return logFactory;
}
}
// Other methods here
}
Upvotes: 1
Views: 230
Reputation: 19847
Think you just need to change:
(MyLogger)MyLogger.MyLogManager.Instance.GetCurrentClassLogger()
Into this:
MyLogger.MyLogManager.Instance.GetCurrentClassLogger<MyLogger>()
Upvotes: 1