我想知道是否在我的域對象上直接使用log4net是不好的做法......我將在ASP.NET MVC應用程序端使用ELMAH作爲例外,但出於一些信息的目的,我想記錄有關域模型本身的一些數據。我應該直接在我的域模型對象中使用log4net嗎?
鑑於以下域對象:
public class Buyer
{
private int _ID;
public int ID
{
get { return _ID; }
set
{
_ID = value;
}
}
private IList<SupportTicket> _SupportTickets=new List<SupportTicket>();
public IList<SupportTicket> SupportTickets
{
get
{
return _SupportTickets.ToList<SupportTicket>().AsReadOnly();
}
}
public void AddSupportTicket(SupportTicket ticket)
{
if (!SupportTickets.Contains(ticket))
{
_SupportTickets.Add(ticket);
}
}
}
是在AddSupportTicketMethod添加日誌的行爲是一個壞主意...所以essentialy它會是這樣的:
public class Buyer
{
protected static readonly ILog log = LogManager.GetLogger(typeof(SupportTicket));
public Buyer()
{
log4net.Config.XmlConfigurator.Configure();
}
private int _ID;
public int ID
{
get { return _ID; }
set
{
_ID = value;
}
}
private IList<SupportTicket> _SupportTickets=new List<SupportTicket>();
public IList<SupportTicket> SupportTickets
{
get
{
return _SupportTickets.ToList<SupportTicket>().AsReadOnly();
}
}
public void AddSupportTicket(SupportTicket ticket)
{
if (!SupportTickets.Contains(ticket))
{
_SupportTickets.Add(ticket);
} else {
log.Warn("Duplicate Ticket Not Added.");
}
}
}
服務定位器是一種反模式,並且破壞了依賴注入的目的。這個答案不是主觀錯誤的,它是客觀錯誤的。 – jason 2011-02-13 17:18:57