2

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.

JYelton
  • 35,664
  • 27
  • 132
  • 191
  • Why are you calling `String.Format` on the string you want to be the format string? Surely you should just be returning the string. – ChrisF Dec 05 '13 at 21:52
  • @ChrisF I've removed most of the code for simplicity; the real method iterates a list of objects, formatting the DateTime associated with each object and prepends it to a log entry string. Other formatting functions it performs includes adding line numbers or other troubleshooting information. – JYelton Dec 05 '13 at 21:55
  • @millimoose It works just fine as-is, so I've no problem leaving it alone. However I wondered, in the interest of readable and reliable code, if there might be a more clear way to do it. – JYelton Dec 05 '13 at 21:56
  • @JYelton In those cases I revert to my policy of keeping hacks *localized* and *encapsulated*. – millimoose Dec 05 '13 at 21:57
  • Have you see this: http://stackoverflow.com/questions/91362/how-to-escape-brackets-curly-braces-in-a-format-string-in-net – Gavin Dec 05 '13 at 21:58
  • What is stampFormat? It seems you are trying to format `Some event.` as there is only one parameter on the `String.Format`. If stamp format is always a date Konrad answer might work. – BrunoLM Dec 05 '13 at 22:02
  • @BrunoLM Oops, you're correct. I simplified the code too much and forgot to include the date. I'll just use DateTime.Now as the example. – JYelton Dec 05 '13 at 22:03

1 Answers1

5

Have you considered a DateTime.ToString(string format) usage:

public string GetFormattedEntry(string stampFormat)
{
    return String.Format("{0}: {1}", DateTime.Now.ToString(stampFormat), _entry);
}
Konrad Kokosa
  • 16,563
  • 2
  • 36
  • 58
  • Not only is this simple, but you read through my mistake of not including the date variable in the original post. Thanks. – JYelton Dec 05 '13 at 22:12
  • @Konrad Kokosa - This is a fantastic answer. If I could upvote it more than once, I would. Regardless, +1. – Brian Dec 05 '13 at 23:37