You could get the information by looking at the stack trace:
StackTrace stackTrace = new StackTrace(); // Get call stack
StackFrame[] stackFrames = stackTrace.GetFrames(); // Get method calls (frames)
string methodName = stackFrames[0].GetMethod().Name; // Get method name
string type = stackFrames[0].GetType().ToString(); // Get the calling type
However, I think the better solution would be to use a more object-oriented approach. Create different loggers that implement the same interface. Pass an instance of your logger to the class depending on what is creating and using the class.
public interface ILogger
{
void Log(string message);
}
public class MyWorkerClass
{
private ILogger m_Logger;
public MyWorkerClass(ILogger logger)
{
m_Logger = logger;
}
public void Work()
{
m_Logger.Log("working")
}
// add other members...
}
public class MyService
{
public void DoSomething()
{
ILogger logger = new MyServiceLogger();
MyWorkerClass worker = new MyWorkerClass(logger);
worker.Work();
}
}