Assembly.GetEntryAssembly() can be null #333
|
An example code is using however, I get this warning:
Is there any other way I could use log4net without a warning? Another option is to use the frame but declaringType can also be null.
Any other solution? |
Replies: 2 comments 3 replies
|
You do not need to call var repository = LogManager.GetRepository();The current implementation resolves the calling assembly internally (and uses log4net's fallback on platforms where caller assembly discovery is unavailable), so there is no nullable value in your code. If you deliberately want the repository associated with a known assembly, use a compile-time-known type instead: var repository = LogManager.GetRepository(typeof(Program).Assembly);For ordinary logging, the usual pattern is simply: private static readonly ILog Log = LogManager.GetLogger(typeof(MyClass));There is no need to inspect a stack frame; that approach is less reliable because frames, methods, and declaring types can be missing or changed by optimization/inlining. |
|
For a default logger field, the usual pattern is still to use the class type directly: private static readonly ILog Log = LogManager.GetLogger(typeof(MyClass));This is safe for static field initialization and gives the logger the class name. If you specifically want a string-based logger name, use |
You do not need to call
Assembly.GetEntryAssembly()here. log4net has a parameterless overload specifically for the default repository:The current implementation resolves the calling assembly internally (and uses log4net's fallback on platforms where caller assembly discovery is unavailable), so there is no nullable value in your code.
If you deliberately want the repository associated with a known assembly, use a compile-time-known type instead:
For ordinary logging, the usual pattern is simply:
There …