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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T12:16:31+00:00 2026-06-14T12:16:31+00:00

Is it possible to configure NLog to write logs to d:\logs if d: exists,

  • 0

Is it possible to configure NLog to write logs to d:\logs if d: exists, and if d: does not exists then log to c:\logs instead?

  • 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-14T12:16:33+00:00Added an answer on June 14, 2026 at 12:16 pm

    I think you could do it with a combination of a custom LayoutRenderer and XML configuration. In XML configuration, you can use LayoutRenderers to specify the filename. For example:

    <target name="logfile" xsi:type="File" fileName="${basedir}/${shortdate}.log" />
    

    The FileTarget object will create a file based on the values of the ${basedir} and ${shortdate} layout renderers.

    You could easily write a custom LayoutRenderer, maybe call it “FindAvailableDrive”, and configure it like this:

    <target name="logfile" xsi:type="File" fileName="${FindAvailableDrive}/logs/${shortdate}.log" />
    

    The FindAvailableDrive LayoutRender could start with a drive letter, say “C:”, and change until it finds a valid drive. To take a step farther, FindAvailableDrive could take one or more parameters that could help define how to search for the valid drive:

    <target name="logfile" xsi:type="File" fileName="${FindAvailableDrive:Drives=DC}/logs/${shortdate}.log" />
    

    In this case, FindAvailableDrive would check drive D then drive C.

    Here is an example (untested) of how you might write such a LayoutRenderer (note that this is based on an existing LayoutRender that I wrote against NLog 1.0, so some of this might not be necessary (or valid) for NLog 2.0):

      [LayoutRenderer("FindAvailableDrive")]
      class FindAvailableDriveLayoutRenderer : LayoutRenderer
      {
        private string validDrive;
    
        [DefaultParameter]
        public string DriveCandidates { get; set; }
    
        protected override void Append(StringBuilder builder, LogEventInfo logEvent)
        {
          builder.Append(FindValidDrive());
        }
    
        protected override int GetEstimatedBufferSize(LogEventInfo logEvent)
        {
          return 2;
        }
    
        private string FindValidDrive()
        {
          if (!String.IsNullOrEmpty(validDrive)) return validDrive;
    
          if (String.IsNullOrEmpty(DriveCandidates))
          {
            if (Directory.Exists("C:"))
              validDrive = "C:";
            else
            if (Directory.Exists("D:"))
              validDrive = "D:";
            //And so on if you want to continue to check for valid drives.
            return validDrive;
          }
    
          validDrive = DriveCandidates.Select(c => string.Format("{0}:", c).Where(d => Directory.Exists(d)).FirstOrDefault();
    
          //What to do if no drives exists?
    
          return validDrive;
        }
      }
    

    You can see my answer to another question here for more information about how to write and use a custom LayoutRenderer in NLog.

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

Sidebar

Related Questions

Is it possible to configure nginx to query a service (not DNS) to determine
Is is possible ot configure Chainsaw to read log files that have been customized
Is it possible to 'configure fb:comments social plugin' to show only comments not allowing
Is it possible to configure a WCF service to use sessions, and also not
Is it possible to configure java.util.logging to compress log files when it rolls onto
Is it possible to configure a WebSetup project to not overwrite config files? Why?
Possible Duplicate: Configure MPI hostsfile to use multiple user identities Does anyone know it
Is it possible to configure the Aptana Remote Systems (FTP) transfer to not include
Is it possible to configure a flash SWF so that it not only is
Is it possible to configure Hiberate/NHibernate not to use the default constructor to create

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.