How to use unity log4net extenstion in different layers



.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;








0















I am using Unity.WwebAPI in my web api project. It is installed at Service level where i have my controller classes. Its all working fine.



Now i am trying to create a wrapper class in separate logging project so that i can use unity log4net extenstion across different layer in my solution.



How can i register unity log4net extenstion in my logging project? I know how to do it in UnityConfig file in service layer but i want to have this in logging project so that my log message method can be accessed in different layers




container.AddNewExtension< Log4NetExtension >();




Here is my test wrapper class;



Unity Log4Net wrapper class



 public class LogWrapper : ILogWrapper

private readonly ILogWrapper _logWrapper;
private ILog _log;

public LogWrapper(ILogWrapper logWrapper)

_logWrapper = logWrapper;


public void LogMessage(string msg)

_log.Info(msg);











share|improve this question

















  • 1





    Why are you injecting logWrapper interface? Isn't it the same class as LogWrapper? Maybe you need to inject ILog instead, and everywhere where you need to use logger (i.e. services/repos) inject the ILogWrapper class

    – Ivvan
    Nov 16 '18 at 11:53

















0















I am using Unity.WwebAPI in my web api project. It is installed at Service level where i have my controller classes. Its all working fine.



Now i am trying to create a wrapper class in separate logging project so that i can use unity log4net extenstion across different layer in my solution.



How can i register unity log4net extenstion in my logging project? I know how to do it in UnityConfig file in service layer but i want to have this in logging project so that my log message method can be accessed in different layers




container.AddNewExtension< Log4NetExtension >();




Here is my test wrapper class;



Unity Log4Net wrapper class



 public class LogWrapper : ILogWrapper

private readonly ILogWrapper _logWrapper;
private ILog _log;

public LogWrapper(ILogWrapper logWrapper)

_logWrapper = logWrapper;


public void LogMessage(string msg)

_log.Info(msg);











share|improve this question

















  • 1





    Why are you injecting logWrapper interface? Isn't it the same class as LogWrapper? Maybe you need to inject ILog instead, and everywhere where you need to use logger (i.e. services/repos) inject the ILogWrapper class

    – Ivvan
    Nov 16 '18 at 11:53













0












0








0


1






I am using Unity.WwebAPI in my web api project. It is installed at Service level where i have my controller classes. Its all working fine.



Now i am trying to create a wrapper class in separate logging project so that i can use unity log4net extenstion across different layer in my solution.



How can i register unity log4net extenstion in my logging project? I know how to do it in UnityConfig file in service layer but i want to have this in logging project so that my log message method can be accessed in different layers




container.AddNewExtension< Log4NetExtension >();




Here is my test wrapper class;



Unity Log4Net wrapper class



 public class LogWrapper : ILogWrapper

private readonly ILogWrapper _logWrapper;
private ILog _log;

public LogWrapper(ILogWrapper logWrapper)

_logWrapper = logWrapper;


public void LogMessage(string msg)

_log.Info(msg);











share|improve this question














I am using Unity.WwebAPI in my web api project. It is installed at Service level where i have my controller classes. Its all working fine.



Now i am trying to create a wrapper class in separate logging project so that i can use unity log4net extenstion across different layer in my solution.



How can i register unity log4net extenstion in my logging project? I know how to do it in UnityConfig file in service layer but i want to have this in logging project so that my log message method can be accessed in different layers




container.AddNewExtension< Log4NetExtension >();




Here is my test wrapper class;



Unity Log4Net wrapper class



 public class LogWrapper : ILogWrapper

private readonly ILogWrapper _logWrapper;
private ILog _log;

public LogWrapper(ILogWrapper logWrapper)

_logWrapper = logWrapper;


public void LogMessage(string msg)

_log.Info(msg);








c# asp.net-web-api unity-container log4net






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 16 '18 at 11:24









user1263981user1263981

1,21963066




1,21963066







  • 1





    Why are you injecting logWrapper interface? Isn't it the same class as LogWrapper? Maybe you need to inject ILog instead, and everywhere where you need to use logger (i.e. services/repos) inject the ILogWrapper class

    – Ivvan
    Nov 16 '18 at 11:53












  • 1





    Why are you injecting logWrapper interface? Isn't it the same class as LogWrapper? Maybe you need to inject ILog instead, and everywhere where you need to use logger (i.e. services/repos) inject the ILogWrapper class

    – Ivvan
    Nov 16 '18 at 11:53







1




1





Why are you injecting logWrapper interface? Isn't it the same class as LogWrapper? Maybe you need to inject ILog instead, and everywhere where you need to use logger (i.e. services/repos) inject the ILogWrapper class

– Ivvan
Nov 16 '18 at 11:53





Why are you injecting logWrapper interface? Isn't it the same class as LogWrapper? Maybe you need to inject ILog instead, and everywhere where you need to use logger (i.e. services/repos) inject the ILogWrapper class

– Ivvan
Nov 16 '18 at 11:53












1 Answer
1






active

oldest

votes


















1














Having the custom logger



Logging Project



public interface ILogger 
void LogMessage(string msg);



so as not to couple your code to the 3rd party logger is a good idea.



Root Project



public class Log4NetWrapper : ILogger 
private readonly ILog log; //log4net

public Log4NetWrapper(ILog log)
this.log = log;


public void LogMessage(string msg)
log.Info(msg);


//...



All that is left is to register your custom logger along with the 3rd party extension to the container in the composition root.



Root Project (Composition Root)



container = new UnityContainer();

//...

container.AddNewExtension<Log4NetExtension>();

//...

container.RegisterType<ILogger, Log4NetWrapper>(new HierarchicalLifetimeManager());


And use your internal logger where needed in the different layers.



public MyService(ILogger log) 
//...



by using this abstraction the other layers do not need to know what is being used behind the scene to do the logging.



Only the composition root would need to be aware of the 3rd party dependency in order to register it with the IoC container.






share|improve this answer

























  • Log4NetExtension is installed in different layer, do i still need to register this in unity container which is in service layer

    – user1263981
    Nov 16 '18 at 12:59






  • 1





    registration tends to happen in the composition root

    – Nkosi
    Nov 16 '18 at 13:00











  • for this i would have to reference log4net in my service project. Whole idea is to have a separate class library for logging which can be used in any other projects.

    – user1263981
    Nov 16 '18 at 13:03






  • 1





    technically only the root project will need to know about the 3rd party logger

    – Nkosi
    Nov 16 '18 at 13:04











  • so do i need to install extension in service project?

    – user1263981
    Nov 16 '18 at 13:06











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%2f53336909%2fhow-to-use-unity-log4net-extenstion-in-different-layers%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









1














Having the custom logger



Logging Project



public interface ILogger 
void LogMessage(string msg);



so as not to couple your code to the 3rd party logger is a good idea.



Root Project



public class Log4NetWrapper : ILogger 
private readonly ILog log; //log4net

public Log4NetWrapper(ILog log)
this.log = log;


public void LogMessage(string msg)
log.Info(msg);


//...



All that is left is to register your custom logger along with the 3rd party extension to the container in the composition root.



Root Project (Composition Root)



container = new UnityContainer();

//...

container.AddNewExtension<Log4NetExtension>();

//...

container.RegisterType<ILogger, Log4NetWrapper>(new HierarchicalLifetimeManager());


And use your internal logger where needed in the different layers.



public MyService(ILogger log) 
//...



by using this abstraction the other layers do not need to know what is being used behind the scene to do the logging.



Only the composition root would need to be aware of the 3rd party dependency in order to register it with the IoC container.






share|improve this answer

























  • Log4NetExtension is installed in different layer, do i still need to register this in unity container which is in service layer

    – user1263981
    Nov 16 '18 at 12:59






  • 1





    registration tends to happen in the composition root

    – Nkosi
    Nov 16 '18 at 13:00











  • for this i would have to reference log4net in my service project. Whole idea is to have a separate class library for logging which can be used in any other projects.

    – user1263981
    Nov 16 '18 at 13:03






  • 1





    technically only the root project will need to know about the 3rd party logger

    – Nkosi
    Nov 16 '18 at 13:04











  • so do i need to install extension in service project?

    – user1263981
    Nov 16 '18 at 13:06















1














Having the custom logger



Logging Project



public interface ILogger 
void LogMessage(string msg);



so as not to couple your code to the 3rd party logger is a good idea.



Root Project



public class Log4NetWrapper : ILogger 
private readonly ILog log; //log4net

public Log4NetWrapper(ILog log)
this.log = log;


public void LogMessage(string msg)
log.Info(msg);


//...



All that is left is to register your custom logger along with the 3rd party extension to the container in the composition root.



Root Project (Composition Root)



container = new UnityContainer();

//...

container.AddNewExtension<Log4NetExtension>();

//...

container.RegisterType<ILogger, Log4NetWrapper>(new HierarchicalLifetimeManager());


And use your internal logger where needed in the different layers.



public MyService(ILogger log) 
//...



by using this abstraction the other layers do not need to know what is being used behind the scene to do the logging.



Only the composition root would need to be aware of the 3rd party dependency in order to register it with the IoC container.






share|improve this answer

























  • Log4NetExtension is installed in different layer, do i still need to register this in unity container which is in service layer

    – user1263981
    Nov 16 '18 at 12:59






  • 1





    registration tends to happen in the composition root

    – Nkosi
    Nov 16 '18 at 13:00











  • for this i would have to reference log4net in my service project. Whole idea is to have a separate class library for logging which can be used in any other projects.

    – user1263981
    Nov 16 '18 at 13:03






  • 1





    technically only the root project will need to know about the 3rd party logger

    – Nkosi
    Nov 16 '18 at 13:04











  • so do i need to install extension in service project?

    – user1263981
    Nov 16 '18 at 13:06













1












1








1







Having the custom logger



Logging Project



public interface ILogger 
void LogMessage(string msg);



so as not to couple your code to the 3rd party logger is a good idea.



Root Project



public class Log4NetWrapper : ILogger 
private readonly ILog log; //log4net

public Log4NetWrapper(ILog log)
this.log = log;


public void LogMessage(string msg)
log.Info(msg);


//...



All that is left is to register your custom logger along with the 3rd party extension to the container in the composition root.



Root Project (Composition Root)



container = new UnityContainer();

//...

container.AddNewExtension<Log4NetExtension>();

//...

container.RegisterType<ILogger, Log4NetWrapper>(new HierarchicalLifetimeManager());


And use your internal logger where needed in the different layers.



public MyService(ILogger log) 
//...



by using this abstraction the other layers do not need to know what is being used behind the scene to do the logging.



Only the composition root would need to be aware of the 3rd party dependency in order to register it with the IoC container.






share|improve this answer















Having the custom logger



Logging Project



public interface ILogger 
void LogMessage(string msg);



so as not to couple your code to the 3rd party logger is a good idea.



Root Project



public class Log4NetWrapper : ILogger 
private readonly ILog log; //log4net

public Log4NetWrapper(ILog log)
this.log = log;


public void LogMessage(string msg)
log.Info(msg);


//...



All that is left is to register your custom logger along with the 3rd party extension to the container in the composition root.



Root Project (Composition Root)



container = new UnityContainer();

//...

container.AddNewExtension<Log4NetExtension>();

//...

container.RegisterType<ILogger, Log4NetWrapper>(new HierarchicalLifetimeManager());


And use your internal logger where needed in the different layers.



public MyService(ILogger log) 
//...



by using this abstraction the other layers do not need to know what is being used behind the scene to do the logging.



Only the composition root would need to be aware of the 3rd party dependency in order to register it with the IoC container.







share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 16 '18 at 13:53

























answered Nov 16 '18 at 12:56









NkosiNkosi

120k17140205




120k17140205












  • Log4NetExtension is installed in different layer, do i still need to register this in unity container which is in service layer

    – user1263981
    Nov 16 '18 at 12:59






  • 1





    registration tends to happen in the composition root

    – Nkosi
    Nov 16 '18 at 13:00











  • for this i would have to reference log4net in my service project. Whole idea is to have a separate class library for logging which can be used in any other projects.

    – user1263981
    Nov 16 '18 at 13:03






  • 1





    technically only the root project will need to know about the 3rd party logger

    – Nkosi
    Nov 16 '18 at 13:04











  • so do i need to install extension in service project?

    – user1263981
    Nov 16 '18 at 13:06

















  • Log4NetExtension is installed in different layer, do i still need to register this in unity container which is in service layer

    – user1263981
    Nov 16 '18 at 12:59






  • 1





    registration tends to happen in the composition root

    – Nkosi
    Nov 16 '18 at 13:00











  • for this i would have to reference log4net in my service project. Whole idea is to have a separate class library for logging which can be used in any other projects.

    – user1263981
    Nov 16 '18 at 13:03






  • 1





    technically only the root project will need to know about the 3rd party logger

    – Nkosi
    Nov 16 '18 at 13:04











  • so do i need to install extension in service project?

    – user1263981
    Nov 16 '18 at 13:06
















Log4NetExtension is installed in different layer, do i still need to register this in unity container which is in service layer

– user1263981
Nov 16 '18 at 12:59





Log4NetExtension is installed in different layer, do i still need to register this in unity container which is in service layer

– user1263981
Nov 16 '18 at 12:59




1




1





registration tends to happen in the composition root

– Nkosi
Nov 16 '18 at 13:00





registration tends to happen in the composition root

– Nkosi
Nov 16 '18 at 13:00













for this i would have to reference log4net in my service project. Whole idea is to have a separate class library for logging which can be used in any other projects.

– user1263981
Nov 16 '18 at 13:03





for this i would have to reference log4net in my service project. Whole idea is to have a separate class library for logging which can be used in any other projects.

– user1263981
Nov 16 '18 at 13:03




1




1





technically only the root project will need to know about the 3rd party logger

– Nkosi
Nov 16 '18 at 13:04





technically only the root project will need to know about the 3rd party logger

– Nkosi
Nov 16 '18 at 13:04













so do i need to install extension in service project?

– user1263981
Nov 16 '18 at 13:06





so do i need to install extension in service project?

– user1263981
Nov 16 '18 at 13:06



















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.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53336909%2fhow-to-use-unity-log4net-extenstion-in-different-layers%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







Popular posts from this blog

Top Tejano songwriter Luis Silva dead of heart attack at 64

ReactJS Fetched API data displays live - need Data displayed static

Evgeni Malkin