I want an end-user to be able to specify a custom datetime format for output to a log file. (It controls how datetime stamps are prepended to each line.)
I am using String.Format with some concatenation which results in a ReSharper warning about unmatched braces. There must be a more appropriate way to handle this but I'm not able to find it.
string _entry = "Some event.";
public string GetFormattedEntry(string stampFormat)
{
return String.Format("{0:" + stampFormat + "}: {1}", DateTime.Now, _entry);
}
The idea is that this can be called using the user-specified format string, for example "yyyy-MM-dd HH:mm:ss", which would result in:
2013-12-05 14:47:57 Some event.
The problem is that ReSharper does not like the split curly bracket usage (and neither do I). It feels like a hack. Is there some better way to pass a string format into a method and use it to format the return value?
(Note that the user-specified string is validated before assuming it's usable.)
Edit:
I've simplified the code example for brevity. The actual method iterates an object collection and formats each accordingly. It also performs other functions involving rich-text formatting, etc, which are not pertinent here.