c# - strange log4net initialization behavior -


i have run strange behavior log4net , wondered if can explain me, love learn more understand it.

inside wpf app.xaml.cs onstartup override method have following code sets log4net property used logging:

// loglocation path subdir in users' appdata roaming dir log4net.globalcontext.properties["loglocation"] = loglocation; logger = logmanager.getlogger(typeof(app)); logger.info("onstartup: " + string.join(" ", e.args)); 

i before create other loggers (or thought).

in config file have following line:

<file type="log4net.util.patternstring" value="%property{loglocation}" /> 

this works specify logging file location. however, have run situation in fails - resulting in logging writing file name "(null)" in executing directory.

what seems cause following pseudo code, in same onstartup method after code above:

anyclass ac = new anyclass(); ac.noop(); 

where anyclass has logger instantiated this:

private static ilog logger = logmanager.getlogger(typeof(anyclass)); 

the reason using pseudo code, can replicate behavior class instantiate , call method on. if no method called, logging path works expected.

a couple of things note:

  1. this works when run in debugger in debug or release mode. error appears when run exe directly or via 'start without debugging'.
  2. if logger not defined statically - problem goes away
  3. if add static constructor anyclass, problem goes away, i.e.:

    static anyclass() {}

  4. if move call ac.noop() secondary method, called onstartup - error goes away.

so, recap, error caused instantiating class static logger initialization, , invoking method on class within onstartup method.

i have several ways work around this, love learn more understand why happening.

i think ac.noop(); creating new instance of logger , new instance not know value of patternstring. hence null value being used.

refer link

public static logger getlogger(string name)  retrieve logger named according value of name parameter.   if named logger exists, existing instance returned.  otherwise, **a new instance** created.  

Comments

Popular posts from this blog

linux - xterm copying to CLIPBOARD using copy-selection causes automatic updating of CLIPBOARD upon mouse selection -

c++ - qgraphicsview horizontal scrolling always has a vertical delta -