Skip to content

Commit 94835f3

Browse files
roei sabagroei sabag
authored andcommitted
first upload
1 parent 18b4aa3 commit 94835f3

File tree

12 files changed

+1890
-0
lines changed

12 files changed

+1890
-0
lines changed

azure-pipelines-csharp.yml

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
# https://docs.microsoft.com/en-us/azure/devops/pipelines/build/variables?view=azure-devops&tabs=yaml
2+
3+
name: Gravity.Abstraction.Logging.$(Year:yyyy).$(Month).$(DayOfMonth)$(Rev:.r)
4+
5+
pool:
6+
name: Default
7+
8+
variables:
9+
- group: browserstack.com
10+
- name: 'buildConfiguration'
11+
value: 'Release'
12+
13+
steps:
14+
- task: DotNetCoreCLI@2
15+
displayName: restore dependencies
16+
inputs:
17+
command: 'restore'
18+
projects: '**/*.csproj'
19+
feedsToUse: 'select'
20+
vstsFeed: '6ed1caf9-de4f-48e8-8276-201533d86a19'
21+
verbosityRestore: 'Diagnostic'
22+
23+
- task: DotNetCoreCLI@2
24+
displayName: build projects
25+
inputs:
26+
command: 'build'
27+
projects: '**/*.csproj'
28+
arguments: '--configuration $(buildConfiguration)'
29+
30+
- powershell: |
31+
[xml]$doc = Get-Content azure-settings.runsettings
32+
$doc.RunSettings.TestRunParameters.ChildNodes.Item(0).value = '$(Grid.Endpoint)'
33+
$doc.RunSettings.TestRunParameters.ChildNodes.Item(1).value = '$(Grid.BasicAuthorization)'
34+
$doc.RunSettings.TestRunParameters.ChildNodes.Item(2).value = '$(Build.BuildNumber)'
35+
$doc.Save("azure-settings-prod.runsettings")
36+
displayName: create test settings > override with production parameters
37+
38+
- task: DotNetCoreCLI@2
39+
displayName: execute automated testing
40+
inputs:
41+
command: 'test'
42+
projects: '**/*[Tt]ests/*.csproj'
43+
arguments: '--configuration $(buildConfiguration) -s azure-settings-prod.runsettings'
44+
testRunTitle: 'Automation Testing --- $(Build.BuildNumber)'
45+
46+
- powershell: |
47+
Remove-Item azure-settings-prod.runsettings
48+
displayName: cleanup
49+
50+
- task: DotNetCoreCLI@2
51+
displayName: create package
52+
inputs:
53+
command: 'pack'
54+
packagesToPack: '**/!(*[Tt]ests)/*.csproj'
55+
versioningScheme: 'byBuildNumber'
56+
verbosityPack: 'Diagnostic'
57+
packDirectory: $(Build.ArtifactStagingDirectory)
58+
59+
- task: NuGetCommand@2
60+
displayName: push package
61+
inputs:
62+
command: 'push'
63+
packagesToPush: '$(Build.ArtifactStagingDirectory)/**/*.nupkg;!$(Build.ArtifactStagingDirectory)/**/*.symbols.nupkg'
64+
nuGetFeedType: 'external'
65+
publishFeedCredentials: 'nuget.org'

azure-settings.runsettings

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<RunSettings>
3+
<TestRunParameters>
4+
<Parameter name="Grid.Endpoint" value="http://localhost:4444/wd/hub" />
5+
<Parameter name="Grid.BasicAuthorization" value="" />
6+
<Parameter name="Build.Number" value="development" />
7+
<Parameter name="Project.Name" value="Gravity API - Web Driver Abstraction Logging" />
8+
</TestRunParameters>
9+
<MSTest>
10+
<Parallelize>
11+
<Workers>5</Workers>
12+
<Scope>MethodLevel</Scope>
13+
</Parallelize>
14+
</MSTest>
15+
</RunSettings>
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
2+
Microsoft Visual Studio Solution File, Format Version 12.00
3+
# Visual Studio Version 16
4+
VisualStudioVersion = 16.0.30011.22
5+
MinimumVisualStudioVersion = 10.0.40219.1
6+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Gravity.Abstraction.Logging", "Gravity.Abstraction.Logging\Gravity.Abstraction.Logging.csproj", "{F37677B9-32AE-4CF1-A77B-297727857290}"
7+
EndProject
8+
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{4BF5C2A6-4748-410A-B5BF-5A8AF0946A9F}"
9+
ProjectSection(SolutionItems) = preProject
10+
..\..\..\azure-pipelines-csharp.yml = ..\..\..\azure-pipelines-csharp.yml
11+
..\..\..\azure-settings.runsettings = ..\..\..\azure-settings.runsettings
12+
EndProjectSection
13+
EndProject
14+
Global
15+
GlobalSection(SolutionConfigurationPlatforms) = preSolution
16+
Debug|Any CPU = Debug|Any CPU
17+
Release|Any CPU = Release|Any CPU
18+
EndGlobalSection
19+
GlobalSection(ProjectConfigurationPlatforms) = postSolution
20+
{F37677B9-32AE-4CF1-A77B-297727857290}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
21+
{F37677B9-32AE-4CF1-A77B-297727857290}.Debug|Any CPU.Build.0 = Debug|Any CPU
22+
{F37677B9-32AE-4CF1-A77B-297727857290}.Release|Any CPU.ActiveCfg = Release|Any CPU
23+
{F37677B9-32AE-4CF1-A77B-297727857290}.Release|Any CPU.Build.0 = Release|Any CPU
24+
EndGlobalSection
25+
GlobalSection(SolutionProperties) = preSolution
26+
HideSolutionNode = FALSE
27+
EndGlobalSection
28+
GlobalSection(ExtensibilityGlobals) = postSolution
29+
SolutionGuid = {F01E51D4-CCD0-4C73-A37F-D43866513D5F}
30+
EndGlobalSection
31+
EndGlobal
Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
1+
/*
2+
* CHANGE LOG - keep only last 5 threads
3+
*/
4+
using System;
5+
using System.Collections.Generic;
6+
using System.Linq;
7+
using System.Text;
8+
9+
namespace Gravity.Abstraction.Logging.Extensions
10+
{
11+
public static class LoggerExtensions
12+
{
13+
/// <summary>
14+
/// Converts a log message into a readable text message that can printed or saved to a file.
15+
/// </summary>
16+
/// <param name="logMessage">Log message to convert.</param>
17+
/// <returns>Readable <see cref="string"/> when can posted to console or into a file.</returns>
18+
public static string AsReadableString(this IDictionary<string, object> logMessage)
19+
{
20+
// constants
21+
const string Application = "Application";
22+
const string Logger = "Logger";
23+
const string LogLevel = "LogLevel";
24+
const string TimeStamp = "TimeStamp";
25+
const string MachineName = "MachineName";
26+
const string Exception = "Exception";
27+
28+
// black list
29+
var blackList = new[] { Application, Logger, LogLevel, TimeStamp, MachineName, Exception };
30+
31+
// get string length
32+
var keys = logMessage.Keys.ToArray();
33+
var maxLength = keys.Max(i => i.Length);
34+
35+
// exclude exception
36+
var pairs = logMessage.Where(i => !blackList.Contains(i.Key));
37+
38+
// build: setup
39+
var level = GetLevel($"{logMessage[LogLevel]}");
40+
var log = new StringBuilder($"{level} - {logMessage[TimeStamp]}{Environment.NewLine}");
41+
AppendDefaults(logMessage, maxLength, log);
42+
43+
// build: iterate
44+
foreach (var pair in pairs)
45+
{
46+
var indent = GetIndent(key: pair.Key, maxLength);
47+
var message = $"{pair.Value}".Replace(Environment.NewLine, string.Empty);
48+
log.Append(indent).Append(pair.Key).Append(": ").AppendLine(message);
49+
}
50+
51+
// exception handler
52+
return logMessage.ContainsKey(Exception)
53+
? AppendExceptionEntry(logMessage, log).ToString()
54+
: log.ToString();
55+
}
56+
57+
private static string GetLevel(string level) => level.ToUpper() switch
58+
{
59+
LogLevel.Debug => "DBG",
60+
LogLevel.Error => "ERR",
61+
LogLevel.Fatal => "FTL",
62+
LogLevel.Information => "INF",
63+
LogLevel.Trace => "TRC",
64+
LogLevel.Warning => "WRN",
65+
_ => "TRC"
66+
};
67+
68+
private static void AppendDefaults(IDictionary<string, object> logMessage, int maxLength, StringBuilder log)
69+
{
70+
// constants
71+
const string Application = "Application";
72+
const string Logger = "Logger";
73+
const string LogLevel = "LogLevel";
74+
const string TimeStamp = "TimeStamp";
75+
const string MachineName = "MachineName";
76+
77+
// defaults
78+
if (logMessage.ContainsKey(Application))
79+
{
80+
AppendKey(logMessage, maxLength, key: Application, log);
81+
}
82+
if (logMessage.ContainsKey(Logger))
83+
{
84+
AppendKey(logMessage, maxLength, key: Logger, log);
85+
}
86+
if (logMessage.ContainsKey(LogLevel))
87+
{
88+
AppendKey(logMessage, maxLength, key: LogLevel, log);
89+
}
90+
if (logMessage.ContainsKey(TimeStamp))
91+
{
92+
AppendKey(logMessage, maxLength, key: TimeStamp, log);
93+
}
94+
if (logMessage.ContainsKey(MachineName))
95+
{
96+
AppendKey(logMessage, maxLength, key: MachineName, log);
97+
}
98+
}
99+
100+
private static void AppendKey(IDictionary<string, object> logMessage, int maxLength, string key, StringBuilder log)
101+
{
102+
log
103+
.Append(GetIndent(key, maxLength))
104+
.Append(key)
105+
.Append(": ")
106+
.Append(logMessage[key])
107+
.AppendLine();
108+
}
109+
110+
private static string GetIndent(string key, int maxLength)
111+
{
112+
// constants
113+
const int Indent = 4;
114+
115+
// get indent for entry
116+
var indent = maxLength - key.Length + Indent;
117+
118+
// results
119+
return new string(' ', indent);
120+
}
121+
122+
private static StringBuilder AppendExceptionEntry(IDictionary<string, object> logMessage, StringBuilder log)
123+
{
124+
return log
125+
.AppendLine("----------------")
126+
.AppendLine("- Exception(s) -")
127+
.AppendLine("----------------")
128+
.Append(logMessage["Exception"]).AppendLine();
129+
130+
}
131+
}
132+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<TargetFrameworks>netstandard2.0;netstandard2.1</TargetFrameworks>
5+
<PackageId>Gravity.Abstraction.Logging</PackageId>
6+
<LangVersion>latest</LangVersion>
7+
<Title>Gravity API - Logging Abstraction</Title>
8+
<Authors>Gravity-API Community</Authors>
9+
<Description>Gravity API, logging abstraction. Allows to create any type of logging output using standard conventions.</Description>
10+
<PackageTags>automation,crawling,crawl,selenium,appium,automation testing,automation test,qa,gravity,gravity api,scrapping,scrap</PackageTags>
11+
<PackageRequireLicenseAcceptance>true</PackageRequireLicenseAcceptance>
12+
<PackageLicenseExpression>MIT</PackageLicenseExpression>
13+
<PackageProjectUrl>https://github.com/gravity-api/gravity-abstraction-logging</PackageProjectUrl>
14+
<NeutralLanguage>en-US</NeutralLanguage>
15+
</PropertyGroup>
16+
17+
</Project>

0 commit comments

Comments
 (0)