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?
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
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.
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:
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:
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:
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):
You can see my answer to another question here for more information about how to write and use a custom LayoutRenderer in NLog.