I have this method:
public void AddNLogConfigurationTypeTagret()
{
var filePath = _loggerModel.file_path_pattern.Replace("\\YYYY\\MM\\DD", "");
var dateTime = DateTime.Now;
filePath += "\\" + dateTime.Year + "\\" + dateTime.Month.ToString() + "\\" + dateTime.Day;
var filePattern = _loggerModel.file_name_pattern.Split('.');
var dateTimeFormat = filePattern[1].Replace("{", "").Replace("}", "").ToString();
var fileName = filePattern[0] + '.' + dateTime.ToString(dateTimeFormat) + "." + filePattern[2];
var fileTargetWithStackTrace = new FileTarget();
fileTargetWithStackTrace.Layout = _loggerModel.layout + "|${stacktrace}";
fileTargetWithStackTrace.Name = FILE_WITH_STACK_TRACE;
fileTargetWithStackTrace.FileName = Path.Combine(filePath, fileName);
_nLogLoggingConfiguration.AddTarget(FILE_WITH_STACK_TRACE, fileTargetWithStackTrace);
var fileTargetWithoutStacktrace = new FileTarget();
fileTargetWithoutStacktrace.Name = FILE_WITHOUT_STACK_TRACE;
fileTargetWithoutStacktrace.Layout = _loggerModel.layout;
fileTargetWithoutStacktrace.FileName = Path.Combine(filePath, fileName);
_nLogLoggingConfiguration.AddTarget(FILE_WITHOUT_STACK_TRACE, fileTargetWithoutStacktrace);
}
It seems that there are similarity inside the code, so I refactored it:
public void AddNLogConfigurationTypeTagret()
{
var filePath = _loggerModel.file_path_pattern.Replace("\\YYYY\\MM\\DD", "");
var dateTime = DateTime.Now;
filePath += "\\" + dateTime.Year + "\\" + dateTime.Month.ToString() + "\\" + dateTime.Day;
var filePattern = _loggerModel.file_name_pattern.Split('.');
var dateTimeFormat = filePattern[1].Replace("{", "").Replace("}", "").ToString();
var fileName = filePattern[0] + '.' + dateTime.ToString(dateTimeFormat) + "." + filePattern[2];
var fileTargetWithStackTrace = new FileTarget();
fileTargetWithStackTrace.Layout = _loggerModel.layout + "|${stacktrace}";
fileTargetWithStackTrace.Name = FILE_WITH_STACK_TRACE;
fileTargetWithStackTrace.FileName = Path.Combine(filePath, fileName);
AddFileTarget(fileTargetWithStackTrace);
var fileTargetWithoutStacktrace = new FileTarget();
fileTargetWithoutStacktrace.Name = FILE_WITHOUT_STACK_TRACE;
fileTargetWithoutStacktrace.Layout = _loggerModel.layout;
fileTargetWithoutStacktrace.FileName = Path.Combine(filePath, fileName);
AddFileTarget(fileTargetWithoutStacktrace);
}
private void AddFileTarget(FileTarget fileTarget)
{
_nLogLoggingConfiguration.AddTarget(fileTarget.Name, fileTarget);
}
Is it good enough?
Edit
By the comment, I made some changes.
public void AddNLogConfigurationTypeTagret()
{
var dateTime = DateTime.Now;
String.Format("\\yyyy\\MM\\dd", dateTime);
var filePath = _loggerModel.file_path_pattern.Replace("\\YYYY\\MM\\DD", "") + dateTime.ToString("\\\\yyyy\\\\MM\\\\dd", CultureInfo.InvariantCulture);
var filePattern = _loggerModel.file_name_pattern.Split('.');
var dateTimeFormat = filePattern[1].Replace("{", "").Replace("}", "").ToString();
var fileName = filePattern[0] + '.' + dateTime.ToString(dateTimeFormat, CultureInfo.InvariantCulture) + "." + filePattern[2];
var fileTargetWithStackTrace = new FileTarget();
fileTargetWithStackTrace.Layout = _loggerModel.layout + "|${stacktrace}";
fileTargetWithStackTrace.Name = FILE_WITH_STACK_TRACE;
fileTargetWithStackTrace.FileName = Path.Combine(filePath, fileName);
AddFileTarget(fileTargetWithStackTrace);
var fileTargetWithoutStacktrace = new FileTarget();
fileTargetWithoutStacktrace.Name = FILE_WITHOUT_STACK_TRACE;
fileTargetWithoutStacktrace.Layout = _loggerModel.layout;
fileTargetWithoutStacktrace.FileName = Path.Combine(filePath, fileName);
AddFileTarget(fileTargetWithoutStacktrace);
}
private void AddFileTarget(FileTarget fileTarget)
{
_nLogLoggingConfiguration.AddTarget(fileTarget.Name, fileTarget);
}
Is it good enough?Without knowing the requirements, there's no way to answer that. \$\endgroup\$fileTargetWithStackTraceandfileTargetWithoutStacktracepart are very similar. So we can combine them together with the clean code principle to reduce the length. The thing is I don't see the obvious change. \$\endgroup\$lotof repetitive coding in the method. Refactoring it to remove the repetition is a good plan. Looking at the new version though, I don't see that any has been removed. All that has changed is wrapping a single line call with a method and then calling it twice. Net addition to the codeAddFileTarget. Net reduction in original method - none. It might be worth looking at moving all the lines in each block out to the new method. \$\endgroup\$