Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I have been fighting with this problem a while now.</p> <p>Really improtant was the Callsite (FullyQualified Namespace) within the logfiles.</p> <p>First, i tryed to get the right logger out of the Stacktrace:</p> <pre><code> [MethodImpl(MethodImplOptions.NoInlining)] private static NLog.Logger GetLogger() { var stackTrace = new StackTrace(false); StackFrame[] frames = stackTrace.GetFrames(); if (null == frames) throw new ArgumentException("Stack frame array is null."); StackFrame stackFrame; switch (frames.Length) { case 0: throw new ArgumentException("Length of stack frames is 0."); case 1: case 2: stackFrame = frames[frames.Length - 1]; break; default: stackFrame = stackTrace.GetFrame(2); break; } Type declaringType = stackFrame.GetMethod() .DeclaringType; return declaringType == null ? LogManager.GetCurrentClassLogger() : LogManager.GetLogger(declaringType.FullName); } </code></pre> <p>But sadly, the Stacktrace with MEF is very long and i cannot clearly identify the correct caller for the Requester of the ILogger.</p> <p>So, instead of injecting the ILogger Interface via Constructor Injection, i have created a ILogFactory Interface, that can get injected via Constructor Injection and call then the Create Method on the Factory</p> <pre><code> public interface ILogFactory { #region Public Methods and Operators /// &lt;summary&gt; /// Creates a logger with the Callsite of the given Type /// &lt;/summary&gt; /// &lt;example&gt; /// factory.Create(GetType()); /// &lt;/example&gt; /// &lt;param name="type"&gt;The type.&lt;/param&gt; /// &lt;returns&gt;&lt;/returns&gt; ILogger Create(Type type); #endregion } </code></pre> <p>And implemented it:</p> <pre><code> using System; using System.ComponentModel.Composition; [Export(typeof(ILogFactory))] [PartCreationPolicy(CreationPolicy.Shared)] public class LogFactory : ILogFactory { #region Public Methods and Operators public ILogger Create(Type type) { var logger = new Logger().CreateLogger(type); return logger; } #endregion } </code></pre> <p>With the ILogger:</p> <pre><code> public interface ILogger { #region Public Properties bool IsDebugEnabled { get; } bool IsErrorEnabled { get; } bool IsFatalEnabled { get; } bool IsInfoEnabled { get; } bool IsTraceEnabled { get; } bool IsWarnEnabled { get; } #endregion #region Public Methods and Operators void Debug(Exception exception); void Debug(string format, params object[] args); void Debug(Exception exception, string format, params object[] args); void Error(Exception exception); void Error(string format, params object[] args); void Error(Exception exception, string format, params object[] args); void Fatal(Exception exception); void Fatal(string format, params object[] args); void Fatal(Exception exception, string format, params object[] args); void Info(Exception exception); void Info(string format, params object[] args); void Info(Exception exception, string format, params object[] args); void Trace(Exception exception); void Trace(string format, params object[] args); void Trace(Exception exception, string format, params object[] args); void Warn(Exception exception); void Warn(string format, params object[] args); void Warn(Exception exception, string format, params object[] args); #endregion } </code></pre> <p>and Implementation of: </p> <pre><code> using System; using NLog; using NLog.Config; /// &lt;summary&gt; /// The logging service. /// &lt;/summary&gt; public class Logger : NLog.Logger, ILogger { #region Fields private string _loggerName; #endregion #region Public Methods and Operators /// &lt;summary&gt; /// The get logging service. /// &lt;/summary&gt; /// &lt;returns&gt; /// The &lt;see cref="ILogger" /&gt;. /// &lt;/returns&gt; public ILogger CreateLogger(Type type) { if (type == null) throw new ArgumentNullException("type"); _loggerName = type.FullName; var logger = (ILogger)LogManager.GetLogger(_loggerName, typeof(Logger)); return logger; } </code></pre> <p>To use it... just inject the ILogFactory and calle the Create Method in a Mefed Importing Constructor:</p> <pre><code> [ImportingConstructor] public MyConstructor( ILogFactory logFactory) { _logger = logFactory.Create(GetType()); } </code></pre> <p>hope this helps</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload