-
Notifications
You must be signed in to change notification settings - Fork 203
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
Setting Global (Variables) Context before logger creation #81
Comments
I'll try to look into this shortly for you; it may be that you've uncovered a scenario that's not properly supported by the new |
Hi Thomas, Have you checked if this works when using Log4Net directly (without Common.Logging)? Perhaps since the log is created prior to the variable being set, Log4Net ignores the change in value, since the log file has already been created. The Common.Logging GlobalContext is only a thin wrapper above Log4Net's "log4net.GlobalContext", so the behaviour should not differ between direct usage of Log4Net vs usage via Common.Logging. Regards, |
TBH i never used properties for log4net logfile configuration. If you want to use a (global context) property in the log4net configuration you must set it prior to the log4net configuration.
Common.Logging configures log4net on the first call to Common.Logging.LogManager.GetLogger(). To access the GlobalVariablesContext we now need to call GetLogger() for an instance of an ILog. This triggers log4net configuration and because we haven't yet set the property it will be read as ´(null)´ One (dirty) way i see is re-do log4net configuration after setting GlobalVariablesContext by calling Reset:
|
@meixger, thanks for weighing in here. My read of your research into this seems to imply that this functionality is actually broken for Log4Net support of GlobalVariablesContext in general (e.g., not only for log file location), right? Just thinking out loud without actually reading the code in Common.Logging (yet), I wonder if we could encapsulate the call to Thoughts welcome (as always!) |
@meixger @sbohlen Does this apply to all usage of the global variables context, or only if used in the log path? Perhaps additional methods of the form: Common.Logging.LogManager.GetXVariablesContext() would be useful in this case. Then you can do this: var globalVariablesContext = Common.Logger.LogManager.GetGlobalVariablesContext(); var log = Common.Logger.LogManager.GetLogger<CurrentClass>(); A workaround is to define the path variables via the Log4Net/NLog config. |
I like that a lot more; not only clear your intent but also much less 'hacky' of a way to solve it. -----Original Message----- @meixger @sbohlen Does this apply to all usage of the global variables context, or only if used in the log path? |
@sbohlen @DannyVarod No, this functionality is actually broken for Log4Net support of GlobalVariablesContext only when used for configuring an appender e.g. log path. It works fine when those global variables are used within log data:
PS: as @DannyVarod suggested, defining the path variables via the Log4Net/NLog config would allow configuring dynamic log path:
|
@meixger - as I assumed. So you can configure the appender via config file and for log parameters, the global variables context does work. If there is need to affect the configuration of the appender via code, then my second suggestion would work, however, this really should count as a new feature request - not a bug fix, when you consider priorities. |
Thanks for your investigation of this behavior. The resolution is that a new feature is necessary for direct access to GlobalContext for appender configuration, isn't it? Like this:
How does the scheduling of priorities for new features look like resp when will this feature appr. be implemented? Thanks for your help. |
I concur with @DannyVarod 's POV that this should be slated for a new feature/enhancement as its a use-case for The recommendation for the time being is going to have to be to employ the 'hack' suggested by @meixger in #81 (comment). re: timing for a more elegant solution to this (perhaps along the lines of what is suggested by @DannyVarod in #81 (comment)), this is probably dependent upon our evaluation re: whether or not this can be affected without introducing a breaking-change to the Common.Logging APIs. Our present plan is to release a Common.Logging 3.1 update in the next week or so that folds in support for NLog 3.2. If this new feature/enhancement can be affected without a need to introduce a breaking change, then it can (probably) fall into that release scope as well. However, if a breaking change will result from this, then its likely to be longer. We just created a rather significant hardship for Common.Logging adopters with the breaking changes in 3.0 (see #74) and since NuGet is still unable to protect consumers from breaking changes, we're less likely to introduce additional breaking changes again (e.g., a 4.0 release to support this feature) this quickly. If anyone is interested in exploring whether this enhancement can be affected without a breaking change to the API necessitating a 4.0 release, we do take pull-requests :) |
Hey, are there any plans to implement this in near future? I'm still using the following hack which makes direct referencing of log4net assembly necessary:
|
Hey, sorry for my spamming, but does no way exists to configure the path for logfiles of log4net with common logging at start/run time without using log4net assembly directly? I couldn't configure it hardcoded in configuration file because the logpath will depend on user account using my application. Thx |
use the command ... log4net.GlobalContext.Properties["LogsDirectory"] = "somedir"; in different appart class but in the same project |
I'm trying to make a dependency injection for the server, directory and filename, any thoughts or recommendations? |
Hey, trying to use log4net1213 with CL 3.0 and feature GlobalVariablesContext does not work in my scenario. For my log4net configuration in app.config I'm using %property{} for changing log file directory dynamically in my bootstrapper. Here part of my app.config:
In combination with log4net specific code all works fine:
But using new GlobalVariablesContext feature like following code will not work:
This happens propably because of setting global context after logger creation. But how can GlobalContext changed before logger creation with CL 3.0?
The text was updated successfully, but these errors were encountered: