I am building an abstract class Log
that will have two subclasses, InfoLog
and ErrorLog
.
My question is whether it is best practice to have all of these classes and their definitions in the same file (Log.cs), or if they should be split up into one file for each class? Or, if there is no clear-cut best practice, what factors should I consider when deciding if I'll divide them into different files?
Below is my current Log.cs file.
using Newtonsoft.Json;
public abstract class Log {
public string LogType;
public string Message;
public string ServiceName;
public string GetJson() {
return JsonConvert.SerializeObject(this,
Formatting.Indented,
new JsonSerializerSettings{ NullValueHandling = NullValueHandling.Ignore});
}
}
public class ErrorLog : Log {
public Logger.ErrorCode ErrorCode;
private string StackTrace;
public ErrorLog(Logger.ErrorCode errorCode,
string serviceName,
string message,
string stackTrace=null)
{
this.ErrorCode = errorCode;
this.ServiceName = serviceName;
this.Message = message;
this.StackTrace = stackTrace;
this.LogType = "ERROR";
}
public string GetStackTrace(){
return this.StackTrace;
}
public void SetStackTrace(string trace) {
this.StackTrace = trace;
}
}
public class InfoLog : Log {
public InfoLog(string serviceName, string message) {
this.ServiceName = serviceName;
this.Message = message;
this.LogType = "INFO";
}
}