Log within whole program










1














How do I log everywhere in the program, without having to declare ILogger parameter in every class? I would like best practice to log everywhere within program.



MyClass(ILogger<MyClass> logger)

_logger = logger;



Should I utilize a static class as described here?



public class ApplicationLogging

private static ILoggerFactory _Factory = null;

public static void ConfigureLogger(ILoggerFactory factory)

factory.AddDebug(LogLevel.None).AddStackify();
factory.AddFile("logFileFromHelper.log"); //serilog file extension


public static ILoggerFactory LoggerFactory

get

if (_Factory == null)

_Factory = new LoggerFactory();
ConfigureLogger(_Factory);

return _Factory;

set _Factory = value;

public static ILogger CreateLogger() => LoggerFactory.CreateLogger();










share|improve this question























  • without having to declare ILogger parameter in every class What is your concern with doing this?
    – mjwills
    Nov 12 at 5:52










  • Take a look at PostSharp : doc.postsharp.net/add-logging
    – Amin Mozhgani
    Nov 12 at 5:57










  • it seems kind of redundant to me, I have around 50+ classes in my program
    – JoeThomas
    Nov 12 at 5:58






  • 1




    Firstly, "I would like best practice to log everywhere within program" don't do that, not everything needs to log, you shouldn't need to be passing a logger to every class, unless this is all top level code. Secondly, the world these days has gone DI, and its very common to inject the logger in when you need it.
    – TheGeneral
    Nov 12 at 5:58







  • 1




    The main issue with using a static is that mocking the logger in unit tests is harder (which, in my mind, is a big issue). If that is not a concern for you, then go ahead.
    – mjwills
    Nov 12 at 6:03















1














How do I log everywhere in the program, without having to declare ILogger parameter in every class? I would like best practice to log everywhere within program.



MyClass(ILogger<MyClass> logger)

_logger = logger;



Should I utilize a static class as described here?



public class ApplicationLogging

private static ILoggerFactory _Factory = null;

public static void ConfigureLogger(ILoggerFactory factory)

factory.AddDebug(LogLevel.None).AddStackify();
factory.AddFile("logFileFromHelper.log"); //serilog file extension


public static ILoggerFactory LoggerFactory

get

if (_Factory == null)

_Factory = new LoggerFactory();
ConfigureLogger(_Factory);

return _Factory;

set _Factory = value;

public static ILogger CreateLogger() => LoggerFactory.CreateLogger();










share|improve this question























  • without having to declare ILogger parameter in every class What is your concern with doing this?
    – mjwills
    Nov 12 at 5:52










  • Take a look at PostSharp : doc.postsharp.net/add-logging
    – Amin Mozhgani
    Nov 12 at 5:57










  • it seems kind of redundant to me, I have around 50+ classes in my program
    – JoeThomas
    Nov 12 at 5:58






  • 1




    Firstly, "I would like best practice to log everywhere within program" don't do that, not everything needs to log, you shouldn't need to be passing a logger to every class, unless this is all top level code. Secondly, the world these days has gone DI, and its very common to inject the logger in when you need it.
    – TheGeneral
    Nov 12 at 5:58







  • 1




    The main issue with using a static is that mocking the logger in unit tests is harder (which, in my mind, is a big issue). If that is not a concern for you, then go ahead.
    – mjwills
    Nov 12 at 6:03













1












1








1







How do I log everywhere in the program, without having to declare ILogger parameter in every class? I would like best practice to log everywhere within program.



MyClass(ILogger<MyClass> logger)

_logger = logger;



Should I utilize a static class as described here?



public class ApplicationLogging

private static ILoggerFactory _Factory = null;

public static void ConfigureLogger(ILoggerFactory factory)

factory.AddDebug(LogLevel.None).AddStackify();
factory.AddFile("logFileFromHelper.log"); //serilog file extension


public static ILoggerFactory LoggerFactory

get

if (_Factory == null)

_Factory = new LoggerFactory();
ConfigureLogger(_Factory);

return _Factory;

set _Factory = value;

public static ILogger CreateLogger() => LoggerFactory.CreateLogger();










share|improve this question















How do I log everywhere in the program, without having to declare ILogger parameter in every class? I would like best practice to log everywhere within program.



MyClass(ILogger<MyClass> logger)

_logger = logger;



Should I utilize a static class as described here?



public class ApplicationLogging

private static ILoggerFactory _Factory = null;

public static void ConfigureLogger(ILoggerFactory factory)

factory.AddDebug(LogLevel.None).AddStackify();
factory.AddFile("logFileFromHelper.log"); //serilog file extension


public static ILoggerFactory LoggerFactory

get

if (_Factory == null)

_Factory = new LoggerFactory();
ConfigureLogger(_Factory);

return _Factory;

set _Factory = value;

public static ILogger CreateLogger() => LoggerFactory.CreateLogger();







c# logging asp.net-core






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 12 at 6:03









Uwe Keim

27.4k30128210




27.4k30128210










asked Nov 12 at 5:51









JoeThomas

677




677











  • without having to declare ILogger parameter in every class What is your concern with doing this?
    – mjwills
    Nov 12 at 5:52










  • Take a look at PostSharp : doc.postsharp.net/add-logging
    – Amin Mozhgani
    Nov 12 at 5:57










  • it seems kind of redundant to me, I have around 50+ classes in my program
    – JoeThomas
    Nov 12 at 5:58






  • 1




    Firstly, "I would like best practice to log everywhere within program" don't do that, not everything needs to log, you shouldn't need to be passing a logger to every class, unless this is all top level code. Secondly, the world these days has gone DI, and its very common to inject the logger in when you need it.
    – TheGeneral
    Nov 12 at 5:58







  • 1




    The main issue with using a static is that mocking the logger in unit tests is harder (which, in my mind, is a big issue). If that is not a concern for you, then go ahead.
    – mjwills
    Nov 12 at 6:03
















  • without having to declare ILogger parameter in every class What is your concern with doing this?
    – mjwills
    Nov 12 at 5:52










  • Take a look at PostSharp : doc.postsharp.net/add-logging
    – Amin Mozhgani
    Nov 12 at 5:57










  • it seems kind of redundant to me, I have around 50+ classes in my program
    – JoeThomas
    Nov 12 at 5:58






  • 1




    Firstly, "I would like best practice to log everywhere within program" don't do that, not everything needs to log, you shouldn't need to be passing a logger to every class, unless this is all top level code. Secondly, the world these days has gone DI, and its very common to inject the logger in when you need it.
    – TheGeneral
    Nov 12 at 5:58







  • 1




    The main issue with using a static is that mocking the logger in unit tests is harder (which, in my mind, is a big issue). If that is not a concern for you, then go ahead.
    – mjwills
    Nov 12 at 6:03















without having to declare ILogger parameter in every class What is your concern with doing this?
– mjwills
Nov 12 at 5:52




without having to declare ILogger parameter in every class What is your concern with doing this?
– mjwills
Nov 12 at 5:52












Take a look at PostSharp : doc.postsharp.net/add-logging
– Amin Mozhgani
Nov 12 at 5:57




Take a look at PostSharp : doc.postsharp.net/add-logging
– Amin Mozhgani
Nov 12 at 5:57












it seems kind of redundant to me, I have around 50+ classes in my program
– JoeThomas
Nov 12 at 5:58




it seems kind of redundant to me, I have around 50+ classes in my program
– JoeThomas
Nov 12 at 5:58




1




1




Firstly, "I would like best practice to log everywhere within program" don't do that, not everything needs to log, you shouldn't need to be passing a logger to every class, unless this is all top level code. Secondly, the world these days has gone DI, and its very common to inject the logger in when you need it.
– TheGeneral
Nov 12 at 5:58





Firstly, "I would like best practice to log everywhere within program" don't do that, not everything needs to log, you shouldn't need to be passing a logger to every class, unless this is all top level code. Secondly, the world these days has gone DI, and its very common to inject the logger in when you need it.
– TheGeneral
Nov 12 at 5:58





1




1




The main issue with using a static is that mocking the logger in unit tests is harder (which, in my mind, is a big issue). If that is not a concern for you, then go ahead.
– mjwills
Nov 12 at 6:03




The main issue with using a static is that mocking the logger in unit tests is harder (which, in my mind, is a big issue). If that is not a concern for you, then go ahead.
– mjwills
Nov 12 at 6:03












1 Answer
1






active

oldest

votes


















2














I think it's not a good idea to use a logger a static dependency. Personally, I would inject it in the constructor of the class. Why? Well, static dependencies in form of a static property have the following problems:



  • In the case of unit tests, they are hard to mock. If you inject the logger in the CTOR you can libs like Moq to change the logging to e.g. stdout (ok, that's also possible with e.g. log4net's config file).


  • Using static properties can make your code "non-determistic". Why? A static property is initialized when the class is referenced the first time. So if you change your code the static property initialization may be called earlier or later. If you've a bug in the initialization it will be hard to find. Why? Because you've no logging.


  • If you want to change the logging framework, you've to find all static references to it. Ok, that's not a that hard task with grep or modern IDEs. However, if you inject the logger dependency the compiler will show (based on the compile errors) which lines of code you've to change (simply remove the logger reference from the solution file).


Hope that helps.






share|improve this answer






















  • thanks, I guess I will pass it as a parameter in every class then, just wanted to confirm
    – JoeThomas
    Nov 12 at 6:06










  • Try to use dependency injection via a DI container like autofac (autofac.org). Pro: if done correctly most parts of your classes are constructed in one place of your program.
    – Moerwald
    Nov 12 at 6:08










  • @JoeThomas, if you're happy with above answer, please mark it as answer for your question. Thx
    – Moerwald
    Nov 12 at 6:21










Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53256524%2flog-within-whole-program%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









2














I think it's not a good idea to use a logger a static dependency. Personally, I would inject it in the constructor of the class. Why? Well, static dependencies in form of a static property have the following problems:



  • In the case of unit tests, they are hard to mock. If you inject the logger in the CTOR you can libs like Moq to change the logging to e.g. stdout (ok, that's also possible with e.g. log4net's config file).


  • Using static properties can make your code "non-determistic". Why? A static property is initialized when the class is referenced the first time. So if you change your code the static property initialization may be called earlier or later. If you've a bug in the initialization it will be hard to find. Why? Because you've no logging.


  • If you want to change the logging framework, you've to find all static references to it. Ok, that's not a that hard task with grep or modern IDEs. However, if you inject the logger dependency the compiler will show (based on the compile errors) which lines of code you've to change (simply remove the logger reference from the solution file).


Hope that helps.






share|improve this answer






















  • thanks, I guess I will pass it as a parameter in every class then, just wanted to confirm
    – JoeThomas
    Nov 12 at 6:06










  • Try to use dependency injection via a DI container like autofac (autofac.org). Pro: if done correctly most parts of your classes are constructed in one place of your program.
    – Moerwald
    Nov 12 at 6:08










  • @JoeThomas, if you're happy with above answer, please mark it as answer for your question. Thx
    – Moerwald
    Nov 12 at 6:21















2














I think it's not a good idea to use a logger a static dependency. Personally, I would inject it in the constructor of the class. Why? Well, static dependencies in form of a static property have the following problems:



  • In the case of unit tests, they are hard to mock. If you inject the logger in the CTOR you can libs like Moq to change the logging to e.g. stdout (ok, that's also possible with e.g. log4net's config file).


  • Using static properties can make your code "non-determistic". Why? A static property is initialized when the class is referenced the first time. So if you change your code the static property initialization may be called earlier or later. If you've a bug in the initialization it will be hard to find. Why? Because you've no logging.


  • If you want to change the logging framework, you've to find all static references to it. Ok, that's not a that hard task with grep or modern IDEs. However, if you inject the logger dependency the compiler will show (based on the compile errors) which lines of code you've to change (simply remove the logger reference from the solution file).


Hope that helps.






share|improve this answer






















  • thanks, I guess I will pass it as a parameter in every class then, just wanted to confirm
    – JoeThomas
    Nov 12 at 6:06










  • Try to use dependency injection via a DI container like autofac (autofac.org). Pro: if done correctly most parts of your classes are constructed in one place of your program.
    – Moerwald
    Nov 12 at 6:08










  • @JoeThomas, if you're happy with above answer, please mark it as answer for your question. Thx
    – Moerwald
    Nov 12 at 6:21













2












2








2






I think it's not a good idea to use a logger a static dependency. Personally, I would inject it in the constructor of the class. Why? Well, static dependencies in form of a static property have the following problems:



  • In the case of unit tests, they are hard to mock. If you inject the logger in the CTOR you can libs like Moq to change the logging to e.g. stdout (ok, that's also possible with e.g. log4net's config file).


  • Using static properties can make your code "non-determistic". Why? A static property is initialized when the class is referenced the first time. So if you change your code the static property initialization may be called earlier or later. If you've a bug in the initialization it will be hard to find. Why? Because you've no logging.


  • If you want to change the logging framework, you've to find all static references to it. Ok, that's not a that hard task with grep or modern IDEs. However, if you inject the logger dependency the compiler will show (based on the compile errors) which lines of code you've to change (simply remove the logger reference from the solution file).


Hope that helps.






share|improve this answer














I think it's not a good idea to use a logger a static dependency. Personally, I would inject it in the constructor of the class. Why? Well, static dependencies in form of a static property have the following problems:



  • In the case of unit tests, they are hard to mock. If you inject the logger in the CTOR you can libs like Moq to change the logging to e.g. stdout (ok, that's also possible with e.g. log4net's config file).


  • Using static properties can make your code "non-determistic". Why? A static property is initialized when the class is referenced the first time. So if you change your code the static property initialization may be called earlier or later. If you've a bug in the initialization it will be hard to find. Why? Because you've no logging.


  • If you want to change the logging framework, you've to find all static references to it. Ok, that's not a that hard task with grep or modern IDEs. However, if you inject the logger dependency the compiler will show (based on the compile errors) which lines of code you've to change (simply remove the logger reference from the solution file).


Hope that helps.







share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 12 at 6:07

























answered Nov 12 at 6:05









Moerwald

2,83841440




2,83841440











  • thanks, I guess I will pass it as a parameter in every class then, just wanted to confirm
    – JoeThomas
    Nov 12 at 6:06










  • Try to use dependency injection via a DI container like autofac (autofac.org). Pro: if done correctly most parts of your classes are constructed in one place of your program.
    – Moerwald
    Nov 12 at 6:08










  • @JoeThomas, if you're happy with above answer, please mark it as answer for your question. Thx
    – Moerwald
    Nov 12 at 6:21
















  • thanks, I guess I will pass it as a parameter in every class then, just wanted to confirm
    – JoeThomas
    Nov 12 at 6:06










  • Try to use dependency injection via a DI container like autofac (autofac.org). Pro: if done correctly most parts of your classes are constructed in one place of your program.
    – Moerwald
    Nov 12 at 6:08










  • @JoeThomas, if you're happy with above answer, please mark it as answer for your question. Thx
    – Moerwald
    Nov 12 at 6:21















thanks, I guess I will pass it as a parameter in every class then, just wanted to confirm
– JoeThomas
Nov 12 at 6:06




thanks, I guess I will pass it as a parameter in every class then, just wanted to confirm
– JoeThomas
Nov 12 at 6:06












Try to use dependency injection via a DI container like autofac (autofac.org). Pro: if done correctly most parts of your classes are constructed in one place of your program.
– Moerwald
Nov 12 at 6:08




Try to use dependency injection via a DI container like autofac (autofac.org). Pro: if done correctly most parts of your classes are constructed in one place of your program.
– Moerwald
Nov 12 at 6:08












@JoeThomas, if you're happy with above answer, please mark it as answer for your question. Thx
– Moerwald
Nov 12 at 6:21




@JoeThomas, if you're happy with above answer, please mark it as answer for your question. Thx
– Moerwald
Nov 12 at 6:21

















draft saved

draft discarded
















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53256524%2flog-within-whole-program%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







這個網誌中的熱門文章

How to read a connectionString WITH PROVIDER in .NET Core?

In R, how to develop a multiplot heatmap.2 figure showing key labels successfully

Museum of Modern and Contemporary Art of Trento and Rovereto