How can I format the Timestamp data that is printed when you set the TraceOutputOptions = TraceOptions.Timestamp?
Im getting something like:
Timestamp=41329240725 (real value that was written on the output text file)
EDITED:
I want something like 10:22:34.32938. How shoud I configure the TextWriterTraceListener to achieve that?
Do you really want to log the time that the message was written? If so, you want to use TraceOptions.DateTime. Note, that according to MSDN, the time is written as UTC.
If you want more control over the format of the time (including if you want it to be expressed in something other than UTC), then you will probably have write your own custom TraceListener, or find one that will do what you want.
One useful add on for System.Diagnostics is Ukadc.Diagnostics. With it you can easily add custom formatting to your log messages (similar to what you can do with log4net and NLog).
Here are some other links to answers that I have provided in the past to logging questions that you might find useful:
When should I use Tracing vs Logger.NET, Enterprise Library, log4net or Ukadc.Diagnostics?
When do I need more than one TraceSource in code?
According to this page
http://msdn.microsoft.com/en-us/library/a10k7w6c.aspx
TraceOptions Timestamp returns the number of ticks, so to convert ticks into time you need to do:
DateTime date = new DateTime(41329240725);
string FormattedDate = date.ToShortDateString();
however 41329240725, seems a little small for ticks (I'm hoping that was just an example)
Related
Tried
${longdate:format=yyyy-MM-ddTHH\\:mm:ss.ffffK}
but its not writing in offset into the file using Nlog.
Tried
${date:format=yyyy-MM-ddTHH\\:mm:ss.ffffK}
it gives the offset and the time output.
Even tried zzz instead of K.
Can we give any other usage for getting the UTC offset in Nlog consoleLayout.Text?
To log DateTimeOffset in database, I use this:
<parameter name="#logged" layout="${longdate}${date:format= K}" />
Please note, that space following = really matters! I checked NLog internal log and here is what I found:
Logged value (with space): 2017-04-21 10:05:48.1868000 +02:00
Logged value (without space): 2017-04-21 10:05:48.1868000
Hope it helped.
${date:format=yyyy-MM-ddTHH\:mm\:ss.ffff}${date:format=%K}
will get you what you want
I know this is an old post but I ran in to the same problem. The pipe solution didn't work for me. I ended up removing the date parameter from my NLog db target and letting the value get set by the db column's default setting which I had set to sysdatetimeoffset() when I originally defined the table.
I have a DropDownList control that is populated with TimeZones from calling:
ReadOnlyCollection<TimeZoneInfo> tzCollection;
tzCollection = TimeZoneInfo.GetSystemTimeZones();
Now I want to add an option to remember a previous user selection in a persistent cookie but I can't figure out which parameter to use for that: I thought to use the time offset from GMT/UTC, but there might be several time zones in the list with the same offset. And using the TimeZoneInfo.Id property also seems bad to me because it is represented by descriptive string such as "Pacific Standard Time", which may pose a problem for localization (in case of foreign languages.)
So any suggestions what shall I use?
TimeZoneInfo.Id is precisely the value to use. You can then fetch it with TimeZoneInfo.FindSystemTimeZoneById.
Even though it looks like a localizable string, it really isn't - I believe you'll get the same result whatever culture you're using. (It's not clear to be honest - I'm finding it hard to persuade any of the properties to give non-English results in test programs.)
I am thinking a library already exists for this, but I need allow my users to create a numbering format for their documents.
For example, let's say we have an RFI from and the user has a specific format the numbering sequence needs to be in. A typical RFI number looks like this for their system: R0000100. The next RFI in line would be R0000101.
Before I set out to creating a formatting engine for numbers such as these, does something already exist that can accommodate this?
Update:
I failed to save the edit to this question. Anyway, I also want to give the users the ability to create their own formats. So, I may have a form where they can input the format: R####### And also allow them to specify the starting integer: in the case 100. Also, I may want to allow them to specify how they want to increment. maybe only by 100s. So the next number may be R0000200. I know this may sound ridiculous, but you never know. That is why I asked if something like this already exists.
If you keep value and format separated, you won't need a library or such a thing.
The numbers would be simple, say, integers i, i.e. 100, 101, 102, that you manage/store however you see fit. The formatting part would simply be a matter of R + i.ToString("0000000"), or if you want to have the format as a string literal string.Format("R{0:0000000}", i).
I know, this might only be an example, but as your question stands, the formatting options, that .NET provides out of the box seem to suffice.
The incrementing of identity field values is most often handled in an RDBMS-style database. This comes with a few benefits, such as built-in concurrency handling. If you want to generate the values yourself, a simple class to get the last-issued value and increment by one would be very easy to create. Make it thread-safe so you don't get any duplicates or gaps and you'll be good to go.
I was doing window services where I'm generating txt files in target path based on some details from a database but I have a problem the service is running too fast!
I was getting same file name in the place of sec variation required so that i can avoid duplicates over there.
code :
using (transactionscope scope = new transactionscope )
{
string nowtime = datetime.now.today.tostring(HHMMss) // it was working fine
}
file should be generates by specific file naming convention !! ex:hhmmss >>> no millisecond
can any one give me exclusive ideas how to face this part?
You can add milliseconds to the filename:
string nowtime = datetime.Now.Today.ToString("HHmmssfff");
See Custom Date and Time Format Strings.
A few notes about the code you posted:
MM is for months, not minutes. You should use lower case mm.
The parameter that ToString takes is a string.
Your code wouldn't compile as it is not correctly cased. Please use code that can be directly used in the future.
Update:
Seeing as you have to use this format, the only other choice is to "slow down" the service.
Adding a:
Thread.Wait(1000);
In the right place (end of loop?) could do the trick.
Alternatively, you can change your code to append to a file if you are still within the same second.
If you are saying that you are creating multiple files with the same name (multiple files in the same second), then I would take the time out to the milliseconds. You can do this with:
DateTime.Today.ToString("HHmmssfff");
The fff denotes the three places to the right of the decimal (thousandths of a second).
I'm hoping that someone has found a way of doing this already or that there is a library already in existence. It's one of those things that would be nice but is in no way necessary for the time being.
The functionality I'm looking for is something like datejs in reverse.
Thanks,
Simon.
Thanks, using something like the dddd example might be a good start towards usability. The more I think about this problem the more it depends on the values being used. I'm specifically dealing with a series of timestamped versions of a document so there is a good chance that they will be clustered. Today isn't so hot if you have saved it three times in the last five minutes.
If I come up with something I'll share it with the community.
Actually, what you really want is the Custom DateTime Format strings:
http://msdn.microsoft.com/en-us/library/8kb3ddd4(VS.71).aspx
DateTime.Now.ToString("ggyyyy$dd-MMM (dddd)")
will return "A.D.2008$06-Nov (Thursday)" if that's what you want.
And to get something closr to datejs ("in forward"), you can use the same strings in DateTime.ParseExact()
http://msdn.microsoft.com/en-us/library/az4se3k1(VS.71).aspx should get you on your way,