Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6608671
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T19:38:04+00:00 2026-05-25T19:38:04+00:00

I have a class that wraps NLog (called NLogger). My logs are saved to

  • 0

I have a class that wraps NLog (called NLogger). My logs are saved to my database.
The thing I’m having a problem with is how do I show where the logging occured.
I have this

<parameter name="@Logger" layout="${callsite}"/>  

but this just shows Core.Logging.Loggers.NLogLogger.Log which is my NlogWrapper not the class which calls my wrapper.

This is my wrapper method

        public void Log(LogType messageType, Type context, string message, Exception exception)
        {
            NLog.Logger logger = NLog.LogManager.GetLogger(context.Name);
            LogLevel logLevel = LogLevel.Info; // Default level to info

            switch (messageType)
            {
                case LogType.Debug:
                    logLevel = LogLevel.Debug;
                    break;
                case LogType.Info:
                    logLevel = LogLevel.Info;
                    break;
                case LogType.Warning:
                    logLevel = LogLevel.Warn;
                    break;
                case LogType.Error:
                    logLevel = LogLevel.Error;
                    break;
                case LogType.Fatal:
                    logLevel = LogLevel.Fatal;
                    break;
                default:
                    throw new ArgumentException("Log message type is not supported");                    
            }

            logger.Log(logLevel, message, exception);
        }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T19:38:04+00:00Added an answer on May 25, 2026 at 7:38 pm

    The problem is that your wrapper is not wrapping correctly. Here is an example of how to wrap NLog correctly, taken directly from the source tree of NLog:

    using System;
    using System.Text;
    using NLog;
    
    namespace LoggerWrapper
    {    
      /// <summary>    
      /// Provides methods to write messages with event IDs - useful for the Event Log target.    
      /// Wraps a Logger instance.    
      /// </summary>    
      class MyLogger    
      {        
        private Logger _logger;        
    
        public MyLogger(string name)        
        {            
          _logger = LogManager.GetLogger(name);        
        }        
    
        public void WriteMessage(string eventID, string message)           
        {            
          ///            
          /// create log event from the passed message            
          ///             
          LogEventInfo logEvent = new LogEventInfo(LogLevel.Info, _logger.Name, message);
    
    
          //
          // set event-specific context parameter            
          // this context parameter can be retrieved using ${event-context:EventID}            
          //            
          logEvent.Context["EventID"] = eventID;            
          //             
          // Call the Log() method. It is important to pass typeof(MyLogger) as the            
          // first parameter. If you don't, ${callsite} and other callstack-related             
          // layout renderers will not work properly.            
          //            
          _logger.Log(typeof(MyLogger), logEvent);        
        }    
      }
    }
    

    The key is passing the type of your logger wrapper to the call to Log. When NLog tries to find the callsite, it goes up the stack until the first calling method whose declaring type is NOT the type passed to the Log call. This will be the code that is actually calling your wrapper.

    In your case, your logger would look something like this:

        public void Log(LogType messageType, Type context, string message, Exception exception)
        {
            NLog.Logger logger = NLog.LogManager.GetLogger(context.Name);
            LogLevel logLevel = LogLevel.Info; // Default level to info
    
            switch (messageType)
            {
                case LogType.Debug:
                    logLevel = LogLevel.Debug;
                    break;
                case LogType.Info:
                    logLevel = LogLevel.Info;
                    break;
                case LogType.Warning:
                    logLevel = LogLevel.Warn;
                    break;
                case LogType.Error:
                    logLevel = LogLevel.Error;
                    break;
                case LogType.Fatal:
                    logLevel = LogLevel.Fatal;
                    break;
                default:
                    throw new ArgumentException("Log message type is not supported");                    
            }
    
            //
            // Build LogEvent here...
            //
            LogEventInfo logEvent = new LogEventInfo(logLevel, context.Name, message);
            logEvent.Exception = exception;
    
            //
            // Pass the type of your wrapper class here...
            //
            logger.Log(typeof(YourWrapperClass), logEvent);
        }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a class called DatabaseHelper that wraps a DbConnection. What's the proper way
I have an Adapter class that wraps an object called X_Session. This adpater expects
I have a class that wraps List<> I have GetValue by index method: public
I have a class that wraps a List and adds functionality for dealing with
In Java, say you have a class that wraps an ArrayList (or any collection)
I have a custom iterator template class that wraps up a std::list iterator. In
Say I have a class that wraps the Controller class: public class MyController :
I have a class that wraps the GetGlobalResourceObject and GetLocalResourceObjet so they can be
I have a static class that wraps some native methods from winspool: public static
I'm working on an asp.net MVC application. I have a class that wraps a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.