My question is I wanna apply strategy pattern to save log

from Image above
I will use replace my class to image
Context = Log Class
Istrategy = ILog
concreteStrategyA = CreateSituation
concreteStrategyB = NameSituation
concreteStrategyC = StatusSituation
[Additional from image]
concreteStrategyD = PriceSituation
concreteStrategyE = DiscountSituation
In ILog have these method
SaveLog(Log log);
each concreteStrategyA,B,C,D,E implement ILog
public class CreateSituation : ILog
{
public void SaveLog(Log log)
{
using(var context = new ProductContext())
{
log.Message = "This product is created";
context.productLog.InsertonSubmit(log);
context.submitChange();
}
}
}
public class NameSituation : ILog
{
public void SaveLog(Log log)
{
using(var context = new ProductContext())
{
log.Message = "this produce has updated name from \"oldProduct\" to \"newProduct\"";
context.productLog.InsertonSubmit(log);
context.submitChange();
}
}
}
public class StatusSituation : ILog
{
public void SaveLog(Log log)
{
using(var context = new ProductContext())
{
log.Message = "this produce has updated status from \"In stock\" to \"Sold Out\"";
context.productLog.InsertonSubmit(log);
context.submitChange();
}
}
}
public class PriceSituation : ILog
{
public void SaveLog(Log log)
{
using(var context = new ProductContext())
{
log.Message = "this produce has updated price from $10 to $150";
context.productLog.InsertonSubmit(log);
context.submitChange();
}
}
}
public class DiscountSituation : Ilog
{
public void SaveLog(Log log)
{
using(var context = new ProductContext())
{
log.Message = "this product has updated discount price from $0 to $20";
context.productLog.InsertonSubmit(log);
context.submitChange();
}
}
}
In the future can have more than present situation
and these classes that I show i a good way to solve these problem
Yes, this is a scenario that could use a strategy pattern.
However, to me it seems to be a just little bit too much to use a completely different logger instance per logging scenario. But without knowing your architecture this is only my personal gut feeling.
As an alternative approach I could imagine implementing a factory for just assembling the appropriate logging message, depending on the logging situation.
[EDIT]
Additionally I would recommend using some framework like log4net that make logging very easy and intuitive. log4net, for example, differentiates between several logging levels like
WARN, INFO, DEBUG, ERROR,orFATALthat allow you to fine-tune your output.