Skip to content

Commit bc66e60

Browse files
committed
Makes date/time format parameterize
1 parent 961f068 commit bc66e60

File tree

2 files changed

+23
-9
lines changed

2 files changed

+23
-9
lines changed

Commands/EnableLogFileCommand.cs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,15 @@
66
namespace PSLogging.Commands
77
{
88
using System.Management.Automation;
9-
9+
1010
[Cmdlet(VerbsLifecycle.Enable, "LogFile")]
1111
public class EnableLogFileCommand : PSCmdlet
1212
{
1313
private ScriptBlock errorCallback;
1414
private LogFile inputObject;
1515
private string path;
1616
private StreamType streams = StreamType.All;
17+
private string dateTimeFormat;
1718

1819
#region Parameters
1920

@@ -53,6 +54,13 @@ public StreamType StreamType
5354
set { streams = value; }
5455
}
5556

57+
[Parameter(ParameterSetName = "New")]
58+
public string DateTimeFormat
59+
{
60+
get { return dateTimeFormat; }
61+
set { dateTimeFormat = value; }
62+
}
63+
5664
#endregion
5765

5866
protected override void EndProcessing()
@@ -61,7 +69,7 @@ protected override void EndProcessing()
6169

6270
if (ParameterSetName == "New")
6371
{
64-
logFile = new LogFile(path, streams, errorCallback);
72+
logFile = new LogFile(path, streams, errorCallback, dateTimeFormat);
6573
WriteObject(logFile);
6674
}
6775
else

LogFile.cs

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,21 +11,25 @@ public class LogFile : HostIOSubscriberBase
1111
{
1212
#region Fields
1313

14-
private const string DateTimeFormat = "r";
14+
//private string DateTimeFormat = "dd-MMM-yyyy - HH:mm:ss";
1515
private readonly string fileName;
1616
private readonly string path;
1717

1818
#endregion
1919

2020
#region Constructors and Destructors
2121

22-
public LogFile(string filename, StreamType streams = StreamType.All, ScriptBlock errorCallback = null)
22+
public LogFile(string filename,
23+
StreamType streams = StreamType.All,
24+
ScriptBlock errorCallback = null,
25+
string dateTimeFormat = "r")
2326
{
2427
fileName = System.IO.Path.GetFileName(filename);
2528
path = System.IO.Path.GetDirectoryName(filename);
2629

2730
Streams = streams;
2831
ErrorCallback = errorCallback;
32+
DateTimeFormat = dateTimeFormat;
2933
}
3034

3135
#endregion
@@ -41,6 +45,8 @@ public string Path
4145

4246
public StreamType Streams { get; set; }
4347

48+
public string DateTimeFormat { get; set; }
49+
4450
#endregion
4551

4652
#region Public Methods and Operators
@@ -65,7 +71,7 @@ public override void WriteDebug(string message)
6571
CheckDirectory();
6672
if (message != String.Empty)
6773
{
68-
message = String.Format("{0,-29} - [D] {1}", DateTime.UtcNow.ToString(DateTimeFormat), message);
74+
message = String.Format("{0,-18} - [D] {1}", DateTime.UtcNow.ToString(DateTimeFormat), message);
6975
}
7076

7177
File.AppendAllText(System.IO.Path.Combine(path, fileName), message);
@@ -92,7 +98,7 @@ public override void WriteError(string message)
9298
CheckDirectory();
9399
if (message.Trim() != String.Empty)
94100
{
95-
message = String.Format("{0,-29} - [E] {1}", DateTime.UtcNow.ToString(DateTimeFormat), message);
101+
message = String.Format("{0,-18} - [E] {1}", DateTime.UtcNow.ToString(DateTimeFormat), message);
96102
}
97103

98104
File.AppendAllText(System.IO.Path.Combine(path, fileName), message);
@@ -119,7 +125,7 @@ public override void WriteOutput(string message)
119125
CheckDirectory();
120126
if (message.Trim() != String.Empty)
121127
{
122-
message = String.Format("{0,-29} - {1}", DateTime.UtcNow.ToString(DateTimeFormat), message);
128+
message = String.Format("{0,-18} - {1}", DateTime.UtcNow.ToString(DateTimeFormat), message);
123129
}
124130

125131
File.AppendAllText(System.IO.Path.Combine(path, fileName), message);
@@ -146,7 +152,7 @@ public override void WriteVerbose(string message)
146152
CheckDirectory();
147153
if (message.Trim() != String.Empty)
148154
{
149-
message = String.Format("{0,-29} - [V] {1}", DateTime.UtcNow.ToString(DateTimeFormat), message);
155+
message = String.Format("{0,-18} - [V] {1}", DateTime.UtcNow.ToString(DateTimeFormat), message);
150156
}
151157

152158
File.AppendAllText(System.IO.Path.Combine(path, fileName), message);
@@ -173,7 +179,7 @@ public override void WriteWarning(string message)
173179
CheckDirectory();
174180
if (message.Trim() != String.Empty)
175181
{
176-
message = String.Format("{0,-29} - [W] {1}", DateTime.UtcNow.ToString(DateTimeFormat), message);
182+
message = String.Format("{0,-18} - [W] {1}", DateTime.UtcNow.ToString(DateTimeFormat), message);
177183
}
178184

179185
File.AppendAllText(System.IO.Path.Combine(path, fileName), message);

0 commit comments

Comments
 (0)