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 8256681
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T01:56:25+00:00 2026-06-08T01:56:25+00:00

I am using log4net in c# console app, and I was wondering if it

  • 0

I am using log4net in c# console app, and I was wondering if it was possible to have each log entry include the elapsed time since the last log entry?

Right now, I am using %-4timestamp (which I believe is the milliseconds since the start of the log) so I can spin through the log later and calculate the difference between an entry and the previous entry. I was hoping there was something I could use to get the elapsed time to appear in each log message though.

Is this possible?

Thanks in advance!

EDIT
To clarify the elapsed time, I mean the amount of time that has elapsed since the last time log4net has logged a message.

For example, if I have this log:

0    [main] INFO  MyApp  - Entering application.
36   [main] DEBUG Com.Foo.Bar  - Did it again!
51   [main] INFO  MyApp  - Exiting application.

Then the additional information I would like would be something like this:

0   (0)  [main] INFO  MyApp  - Entering application.
36  (36) [main] DEBUG Com.Foo.Bar  - Did it again!
51  (15) [main] INFO  MyApp  - Exiting application.

Where the extra value I added in between parenthesis is the elapsed time since the last log.

  • 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-06-08T01:56:28+00:00Added an answer on June 8, 2026 at 1:56 am

    Why not just keep track of it and add it yourself?

    Something like:

    public static class LoggingWrapper
        {
            private static ILog logger;
            private DateTime timeOfLastLog;
    
            static LoggingWrapper()
            {
                logger = LogManager.GetLogger(typeof(Program));
                logger.Info("Logger initialized");
                timeOfLastLog = DateTime.Now;
            }
    
            public static void Debug(string Message)
            {
                logger = LogManager.GetLogger("YourDebugLoggerName");
                TimeSpan differential = DateTime.Now - timeOfLastLog();
                logger.Debug(Message + "-----" + differential.ToString());
            }
        }
    

    Obviously youd want to customize a bit, but this is the basic idea. I always use a wrapper class to do logging, just because it adds customization capabilities. For example, I use it to do custom exception logging like this:

    public static void LogException(Exception ex)
    {
        logger = LogManager.GetLogger("NHibernate.SQL");
        logger.Error(ex.Message + Environment.NewLine + ex.InnerException + Environment.NewLine + ex.StackTrace, ex);
    }
    

    That way, when I want to log exception details, I can do something like:

    try
    {
        ThrowAnException();
    }
    catch(Exception ex)
    {
        LoggingWrapper.LogException(ex);
    }
    

    instead of:

    try
    {
        ThrowAnException();
    }
    catch(Exception ex)
    {
        LoggingWrapper.Error(ex.Message + Environment.NewLine + ex.InnerException + Environment.NewLine + ex.StackTrace);
    }
    

    And that’s just one example. There are quite a few other things that make this useful.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have used log4net in winforms before. First time using with a wpf console
I have a Windows form App which write to log files using log4net. The
I have a console app using log4net (via Castle Windsor). Everything logs fine to
I am using log4net to log to the console, and the %date conversionPattern value
I'm using log4net to log my web app's progress, using Log4PostSharp to AOP-injectify all
I am using log4net for the first time and have followed the documentation using
I'm using log4net to log in my app. My FileAppender is working fine, but
I am using log4net 1.2.10.0. I have extended ILog and LogManager to include a
Using log4net we would like to log all calls to our ASP.NET MVC controller
I'm using log4Net for my logging. I also have the following set... <log4net debug=true>

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.