Friday, November 13, 2009

The Simplest Possible Ninject log4net Binding

I’ve been using Ninject more and more. I also use log4net for logging. But how to do I get my log4net peanut butter into my Ninject chocolate? Like this:

Bind<ILog>().ToMethod(x => LogManager.GetLogger(x.Request.ParentContext.Plan.Type)); 

As far as I can tell, this is the simplest possible way to get Ninject to build an ILog object for dependency injection. NOTE! This is only valid for dependency injection! If you just call:


kernel.Get<ILog>();

You will be less than satisfied with the results… However, this works brilliantly:


public class Notifier : INotifier
{
private readonly ILog _log;

public Notifier(ILog log)
{
_log = log;
}
}

No wrappers, static classes or helper objects! If I ever meet Nate Kohari, I’m going to give him a big ol' hug. I may even tear up a little bit…

38 comments:

佩璇 said...

Never put both feet in your mouth at the same time, because then you will not have a leg to stand on.............................................

王志竹 said...

原來這世上能跟你共同領略一個笑話的人竟如此難得........................................

DeonKilgore said...

Hello~Nice to meet you~..................................................

鈺禎 said...

沒有友情,人生何樂? ..................................................

RexTemples0217 said...

思想與理論,貴呼先於行動,但行動較思想或理論更高貴..................................................

宛慧桓玲 said...

非常感謝~3Q~....................................................

淑娟 said...

cool blog,期待更新........................................

韋于倫成 said...

如此活躍的文字, 妳好棒哦! ....................................................

Domini said...

要保持更新呦,加油!!!期待你的新文章!!!........................................

育德 said...

憂能傷身,保重哦! ....................................................

于呈均名 said...

Today is the first day of the rest of your life. ............................................................

文君 said...

Necessity is the mother of invention.............................................................

佩芳 said...

No pains, no gains...................................................................

俊毅俊毅 said...

初次拜訪,踩踩您的格子,跟您拜個碼頭囉~~.................................................................

Elvis湘均Kasp湘均 said...

知識可以傳授,智慧卻不行。每個人必須成為他自己。......................................................................

江婷 said...

死亡是悲哀的,但活得不快樂更悲哀。.................................................................

啟均 said...

當一個人內心能容納兩樣相互衝突的東西,這個人便開始變得有價值了。............................................................

吳婷婷 said...

向著星球長驅直進的人,反比踟躕在峽路上的人,更容易達到目的。............................................................

香昱信張君林 said...

來幫推 你個blog影d相真係好靚,係我至愛~ ..................................................................

健豪健豪 said...

woo ~ woo ~ thx alotsˋε ˊ..................................................................

原秋原秋 said...

文章是心情的反應~~祝妳天天寫的都是讓人開心的好文章哦!!............................................................

anthonyjensen張anthonyjensen欣虹 said...

Words are not living in dictionary. Words are living in mind.............................................................

佳皓佳皓 said...

脾氣與嘴巴不好,就算心地再好,也不算好人~~~..................................................

芳瑜芳瑜 said...

人並不是生來要吃敗仗的。人可被毀滅,但不可被擊倒。.................................................................

蔡靜芳蔡靜芳 said...

There is no key to happiness, only a ladder..................................................................

謝俊柯凡豐德 said...

It is easier to get than to keep it.......................................................................

鄭雅雯 said...

來看你囉~加油~~!!............................................................

Tammy said...

Thank you for sharing this information,i am very happy with your updates.

small business logo design | design logo web | corporate logo designs | company logo designs

胡美謝宗偉惠 said...

所有的資產,在不被諒解時,都成了負債.................................................................

佳呈均慧 said...

相逢即是有緣~~留個言問候一聲,祝您平安順利...............................................................

文王廷 said...

困難的背後,隱藏著通往成功的階梯............................................................

蕙陳春宗陳穎 said...

Subtlety is better than force. ......................................................................

童建李宜昌智 said...

時間就是塑造生命的材料。..................................................... ............

國林林林林維 said...

文章是心情的反應~~祝妳天天寫的都是讓人開心的好文章哦!!...............................................................

7987 said...

人生像一杯茶,若一飲而盡,會提早見到杯底..................................................

于庭吳 said...

來囉~先問聲安.................................[/url]...............

蕾蕾 said...

愛情是盲目的,但婚姻恢復了它的視力。......................................................................

司冯欣 said...

睇完之後覺得有d頓悟..感謝大大分享..˙ 3˙............................................................