If you are mainly interested in using your NLog wrapper but still being able to maintain callsite information, see my answer to an earlier question here:
Nlog Callsite is wrong when wrapper is used
In brief, you can use the NLog Log method and pass the type of your wrapper. Then, if you use the NLog callsite LayoutRenderer, NLog will be able to figure out the callsite information without your having to figure it out for yourself.
So, your LogWrapper might have a method like this:
public static void Informational(string fmt, Exception exception)
{
LogEventInfo le = new LogEventInfo(LogLevel.Info, logger.Name, null, fmt, exception.ToString());
logger.Log(typeof(LogWrapper), le);
}
The key is passing the type of your wrapper (typeof(LogWrapper)
) as the first argument to Logger.Log
. NLog uses that value to traverse up the call stack until it sees that type as the DeclaringType of the current MethodInfo. NLog treats that stack frame as the last stack frame before the actual callsite, so NLog goes up one more level after seeing it.
You should know that NLog also has an Exception LayoutRenderer, so you don't have to use exception.ToString() yourself.
While the question that Daniel Hilgarth linked to in his comment has some interesting code, I think that you should be very careful about adding a bunch of code to determine information that NLog can get for you for "free". If you only need it for logging purposes, I would recommend letting NLog figure it out for you. If you need for some other purpose, then you probably have no choice but to figure it out for yourself.
On a side note, I would also recommend not making your logging calls using this style:
LogWrapper.Informational(string.Format(" {0} starts {1}",
MethodBase.GetCurrentMethod().DeclaringType.Name, MethodBase.GetCurrentMethod().Name));
If your LogWrapper.Informational method delegates down to NLog's Logger.Info, then you are doing some extra work in the case where logging is not turned on or if the logging level is less than Info (e.g. Warn, Error, Fatal). If, due to the current logging level settings, the statement would not actually get logged, you are still formatting the string and you are making two relatively expensive calls to get the callsite information.