Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

RollingFileTraceListener to use environment variable for root directory of the log files #15

Open
sgryphon opened this issue Jul 18, 2017 · 1 comment

Comments

@sgryphon
Copy link
Owner

app.config does not support Windows environment variables. There could be a few solutions:
The application alters the config file at startup. The drawbak is, this requires high privilege, and miss trace info before the app codes run.
Installer to alter the config during installation. This requires a lot scripting.
It will be more convenient to have
initializeData="%ProgramData%\My Company{ApplicationName}-{LocalDateTime:yyyy-MM-dd}.log"

Documentation later upon next release:
You may use Windows environment variable in front the the file path. For example:
initializeData="%ProgramData%\My Company{ApplicationName}-{LocalDateTime:yyyy-MM-dd}.log"

Please note, app.config does not support interpreting environment variables, thus you can not apply the same to TextWriterTraceListener in System.Diagnostics.

@Erich-Stehr
Copy link

To get something similar to work, I derived classes from TextWriterTraceListener (available in all versions) and XmlWriterTraceListener (available in .Net 2.0+, .NET Core 3+):

using System;
using System.Diagnostics;

// To the extent possible under law, Erich Stehr has waived all copyright and related or neighboring rights to this work. This work is published from: United States.

namespace ns1 {
    public class TextWriterTraceListener : System.Diagnostics.TextWriterTraceListener
    {
        public TextWriterTraceListener(string initializeData) : base(Environment.ExpandEnvironmentVariables(initializeData)) {}
        public TextWriterTraceListener(string initializeData, string name) : base(Environment.ExpandEnvironmentVariables(initializeData), name) {}
    }
}

With this exposed in a myprog.exe, the App.config simply needed a shared listener with type="ns1.TextWriterTraceListener, myprog" initializeData="%ProgramData%\My Company{ApplicationName}-{LocalDateTime:yyyy-MM-dd}.log" to place the log appropriately.

Obviously, I'm hiding the other constructors, but I can't use those from the App.config, and can inject the expanded path into the System.Diagnostics class itself from code.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants