How to use unity log4net extenstion in different layers
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
add a comment |
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
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
add a comment |
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
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
c# asp.net-web-api unity-container log4net
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
add a comment |
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
add a comment |
1 Answer
1
active
oldest
votes
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.
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
|
show 16 more comments
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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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.
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
|
show 16 more comments
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.
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
|
show 16 more comments
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.
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.
edited Nov 16 '18 at 13:53
answered Nov 16 '18 at 12:56
NkosiNkosi
120k17139204
120k17139204
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
|
show 16 more comments
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
|
show 16 more comments
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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