0

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";
    }
}
ThomasJazz
  • 99
  • 9
  • Each type should be in a separate file. – Emanuel Vintilă Mar 24 '20 at 17:55
  • @EmanuelVintilă Why? – D Stanley Mar 24 '20 at 17:58
  • Coding style questions are off-topic on SO as they are completely opinion-based. Pick a style guide you like (you can start with https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/inside-a-program/coding-conventions and add whatever you feel necessary or find another one) – Alexei Levenkov Mar 24 '20 at 18:00
  • Note that the marked duplicate has dissenting answers, so take it more to learn the pros/cons rather than a de facto answer. – D Stanley Mar 24 '20 at 18:00

0 Answers0