Skip to content

Commit

Permalink
Adding log prefixkey to html logger (#2204)
Browse files Browse the repository at this point in the history
* added logprefix
* tests added
* added error when both logfileprefix and logfilevalue is given
* filename edited when target framework is null
* formatted
* added framework null case in trxlogger
  • Loading branch information
hvinett authored and singhsarab committed Oct 23, 2019
1 parent 89d102d commit 7ba379f
Show file tree
Hide file tree
Showing 21 changed files with 219 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,12 @@ public static class Constants
/// <summary>
/// Log file parameter key
/// </summary>
public const string LogFileNameKey = "LogFileName";
public const string LogFileNameKey = "LogFileName";

/// <summary>
/// Log file prefix parameter key
/// </summary>
public const string LogFilePrefixKey = "LogFilePrefix";

public static readonly TestProperty ExecutionIdProperty = TestProperty.Register("ExecutionId", ExecutionIdPropertyIdentifier, typeof(Guid), TestPropertyAttributes.Hidden, typeof(TestResult));

Expand Down
35 changes: 29 additions & 6 deletions src/Microsoft.TestPlatform.Extensions.HtmlLogger/HtmlLogger.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ namespace Microsoft.VisualStudio.TestPlatform.Extensions.HtmlLogger

using HtmlResource = Resources.Resources;
using HtmlLoggerConstants = Constants;
using NuGet.Frameworks;

/// <summary>
/// Logger for generating Html.
Expand Down Expand Up @@ -137,6 +138,14 @@ public void Initialize(TestLoggerEvents events, Dictionary<string, string> param
}

parametersDictionary = parameters;

if (parameters.TryGetValue(HtmlLoggerConstants.LogFilePrefixKey, out string logFilePrefixValue) && parameters.TryGetValue(HtmlLoggerConstants.LogFileNameKey, out string logFileNameValue))
{
var htmlParameterErrorMsg = string.Format(CultureInfo.CurrentCulture, HtmlResource.PrefixAndNameProvidedError);
EqtTrace.Error(htmlParameterErrorMsg);
throw new ArgumentException(htmlParameterErrorMsg);
}

this.Initialize(events, parameters[DefaultLoggerParameterNames.TestRunDirectory]);
}

Expand All @@ -153,7 +162,7 @@ public void TestMessageHandler(object sender, TestRunMessageEventArgs e)
switch (e.Level)
{
case TestMessageLevel.Informational:
if(TestRunDetails.RunLevelMessageInformational == null)
if (TestRunDetails.RunLevelMessageInformational == null)
{
TestRunDetails.RunLevelMessageInformational = new List<string>();
}
Expand Down Expand Up @@ -273,11 +282,25 @@ public void TestRunCompleteHandler(object sender, TestRunCompleteEventArgs e)
PassPercentage = (PassedTests * 100) / TotalTests,
TotalRunTime = GetFormattedDurationString(e.ElapsedTimeInRunningTests),
};
var isLogFileNameParameterExists = parametersDictionary.TryGetValue(HtmlLoggerConstants.LogFileNameKey,
out string logFileNameValue);
if (isLogFileNameParameterExists && !string.IsNullOrWhiteSpace(logFileNameValue))
if (this.parametersDictionary.TryGetValue(HtmlLoggerConstants.LogFilePrefixKey, out string logFilePrefixValue) && !string.IsNullOrWhiteSpace(logFilePrefixValue))
{

var framework = this.parametersDictionary[DefaultLoggerParameterNames.TargetFramework];
if (framework != null)
{
framework = NuGetFramework.Parse(framework).GetShortFolderName();
logFilePrefixValue = logFilePrefixValue + "_" + framework;
}

logFilePrefixValue = logFilePrefixValue + DateTime.Now.ToString("_yyyyMMddHHmmss", DateTimeFormatInfo.InvariantInfo) + $".{HtmlLoggerConstants.HtmlFileExtension}";
this.HtmlFilePath = Path.Combine(TestResultsDirPath, logFilePrefixValue);
}
else
{
HtmlFilePath = Path.Combine(TestResultsDirPath, logFileNameValue);
if (parametersDictionary.TryGetValue(HtmlLoggerConstants.LogFileNameKey, out string logFileNameValue) && !string.IsNullOrWhiteSpace(logFileNameValue))
{
this.HtmlFilePath = Path.Combine(TestResultsDirPath, logFileNameValue);
}
}

PopulateHtmlFile();
Expand Down Expand Up @@ -410,7 +433,7 @@ internal string GetFormattedDurationString(TimeSpan duration)
time.Add(duration.Milliseconds + "ms");
}
}
}
}

return time.Count == 0 ? "< 1ms" : string.Join(" ", time);
}
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -123,4 +123,7 @@
<data name="HtmlLoggerError" xml:space="preserve">
<value>Html Logger Error : {0}</value>
</data>
<data name="PrefixAndNameProvidedError" xml:space="preserve">
<value>The parameters LogFileName and LogFilePrefix cannot be given together.</value>
</data>
</root>
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,11 @@
<target state="new">Html Logger Error : {0}</target>
<note></note>
</trans-unit>
<trans-unit id="PrefixAndNameProvidedError">
<source>The parameters LogFileName and LogFilePrefix cannot be given together.</source>
<target state="new">The parameters LogFileName and LogFilePrefix cannot be given together.</target>
<note></note>
</trans-unit>
</body>
</file>
</xliff>
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,11 @@
<target state="new">Html Logger Error : {0}</target>
<note></note>
</trans-unit>
<trans-unit id="PrefixAndNameProvidedError">
<source>The parameters LogFileName and LogFilePrefix cannot be given together.</source>
<target state="new">The parameters LogFileName and LogFilePrefix cannot be given together.</target>
<note></note>
</trans-unit>
</body>
</file>
</xliff>
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,11 @@
<target state="new">Html Logger Error : {0}</target>
<note></note>
</trans-unit>
<trans-unit id="PrefixAndNameProvidedError">
<source>The parameters LogFileName and LogFilePrefix cannot be given together.</source>
<target state="new">The parameters LogFileName and LogFilePrefix cannot be given together.</target>
<note></note>
</trans-unit>
</body>
</file>
</xliff>
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,11 @@
<target state="new">Html Logger Error : {0}</target>
<note></note>
</trans-unit>
<trans-unit id="PrefixAndNameProvidedError">
<source>The parameters LogFileName and LogFilePrefix cannot be given together.</source>
<target state="new">The parameters LogFileName and LogFilePrefix cannot be given together.</target>
<note></note>
</trans-unit>
</body>
</file>
</xliff>
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,11 @@
<target state="new">Html Logger Error : {0}</target>
<note></note>
</trans-unit>
<trans-unit id="PrefixAndNameProvidedError">
<source>The parameters LogFileName and LogFilePrefix cannot be given together.</source>
<target state="new">The parameters LogFileName and LogFilePrefix cannot be given together.</target>
<note></note>
</trans-unit>
</body>
</file>
</xliff>
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,11 @@
<target state="new">Html Logger Error : {0}</target>
<note></note>
</trans-unit>
<trans-unit id="PrefixAndNameProvidedError">
<source>The parameters LogFileName and LogFilePrefix cannot be given together.</source>
<target state="new">The parameters LogFileName and LogFilePrefix cannot be given together.</target>
<note></note>
</trans-unit>
</body>
</file>
</xliff>
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,11 @@
<target state="new">Html Logger Error : {0}</target>
<note></note>
</trans-unit>
<trans-unit id="PrefixAndNameProvidedError">
<source>The parameters LogFileName and LogFilePrefix cannot be given together.</source>
<target state="new">The parameters LogFileName and LogFilePrefix cannot be given together.</target>
<note></note>
</trans-unit>
</body>
</file>
</xliff>
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,11 @@
<target state="new">Html Logger Error : {0}</target>
<note></note>
</trans-unit>
<trans-unit id="PrefixAndNameProvidedError">
<source>The parameters LogFileName and LogFilePrefix cannot be given together.</source>
<target state="new">The parameters LogFileName and LogFilePrefix cannot be given together.</target>
<note></note>
</trans-unit>
</body>
</file>
</xliff>
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,11 @@
<target state="new">Html Logger Error : {0}</target>
<note></note>
</trans-unit>
<trans-unit id="PrefixAndNameProvidedError">
<source>The parameters LogFileName and LogFilePrefix cannot be given together.</source>
<target state="new">The parameters LogFileName and LogFilePrefix cannot be given together.</target>
<note></note>
</trans-unit>
</body>
</file>
</xliff>
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,11 @@
<target state="new">Html Logger Error : {0}</target>
<note></note>
</trans-unit>
<trans-unit id="PrefixAndNameProvidedError">
<source>The parameters LogFileName and LogFilePrefix cannot be given together.</source>
<target state="new">The parameters LogFileName and LogFilePrefix cannot be given together.</target>
<note></note>
</trans-unit>
</body>
</file>
</xliff>
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,11 @@
<target state="new">Html Logger Error : {0}</target>
<note></note>
</trans-unit>
<trans-unit id="PrefixAndNameProvidedError">
<source>The parameters LogFileName and LogFilePrefix cannot be given together.</source>
<target state="new">The parameters LogFileName and LogFilePrefix cannot be given together.</target>
<note></note>
</trans-unit>
</body>
</file>
</xliff>
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@
<target state="new">Html Logger Error : {0}</target>
<note></note>
</trans-unit>
<trans-unit id="PrefixAndNameProvidedError">
<source>The parameters LogFileName and LogFilePrefix cannot be given together.</source>
<target state="new">The parameters LogFileName and LogFilePrefix cannot be given together.</target>
<note></note>
</trans-unit>
</body>
</file>
</xliff>
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,11 @@
<target state="new">Html Logger Error : {0}</target>
<note></note>
</trans-unit>
<trans-unit id="PrefixAndNameProvidedError">
<source>The parameters LogFileName and LogFilePrefix cannot be given together.</source>
<target state="new">The parameters LogFileName and LogFilePrefix cannot be given together.</target>
<note></note>
</trans-unit>
</body>
</file>
</xliff>
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,11 @@
<target state="new">Html Logger Error : {0}</target>
<note></note>
</trans-unit>
<trans-unit id="PrefixAndNameProvidedError">
<source>The parameters LogFileName and LogFilePrefix cannot be given together.</source>
<target state="new">The parameters LogFileName and LogFilePrefix cannot be given together.</target>
<note></note>
</trans-unit>
</body>
</file>
</xliff>
11 changes: 7 additions & 4 deletions src/Microsoft.TestPlatform.Extensions.TrxLogger/TrxLogger.cs
Original file line number Diff line number Diff line change
Expand Up @@ -489,12 +489,15 @@ private void DeriveTrxFilePath()
{
if (!string.IsNullOrWhiteSpace(logFilePrefixValue))
{
var framework = this.parametersDictionary[DefaultLoggerParameterNames.TargetFramework] ?? string.Empty;
framework = NuGetFramework.Parse(framework).GetShortFolderName();
var framework = this.parametersDictionary[DefaultLoggerParameterNames.TargetFramework];
if (framework != null)
{
framework = NuGetFramework.Parse(framework).GetShortFolderName();
logFilePrefixValue = logFilePrefixValue + "_" + framework;
}

logFilePrefixValue = logFilePrefixValue.Replace(".trx", string.Empty) + "_" + framework + DateTime.Now.ToString("_yyyyMMddHHmmss", DateTimeFormatInfo.InvariantInfo) + this.trxFileExtension;
logFilePrefixValue = logFilePrefixValue + DateTime.Now.ToString("_yyyyMMddHHmmss", DateTimeFormatInfo.InvariantInfo) + this.trxFileExtension;
this.trxFilePath = Path.Combine(this.testResultsDirPath, logFilePrefixValue);

return;
}
}
Expand Down
Loading

0 comments on commit 7ba379f

Please sign in to comment.