Browse Source

init

master
Tobias Hübner 5 months ago
commit
72e3bef1ed
11 changed files with 460 additions and 0 deletions
  1. +5
    -0
      .gitignore
  2. +31
    -0
      Logging.sln
  3. +3
    -0
      Logging/.gitignore
  4. +56
    -0
      Logging/Logging.csproj
  5. +36
    -0
      Logging/Properties/AssemblyInfo.cs
  6. +182
    -0
      Logging/logging.cs
  7. +3
    -0
      T_logging/.gitignore
  8. +20
    -0
      T_logging/Properties/AssemblyInfo.cs
  9. +45
    -0
      T_logging/T_logging.cs
  10. +74
    -0
      T_logging/T_logging.csproj
  11. +5
    -0
      T_logging/packages.config

+ 5
- 0
.gitignore View File

@@ -0,0 +1,5 @@
.vs
packages
TestResults
bin
obj

+ 31
- 0
Logging.sln View File

@@ -0,0 +1,31 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 16
VisualStudioVersion = 16.0.30204.135
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "logging", "Logging\logging.csproj", "{AF84FB43-ACBE-40DC-A75A-E4CCF01E8ADD}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "T_logging", "T_logging\T_logging.csproj", "{613CDE20-C71A-4EE1-97E8-226DAEC69C37}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{AF84FB43-ACBE-40DC-A75A-E4CCF01E8ADD}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{AF84FB43-ACBE-40DC-A75A-E4CCF01E8ADD}.Debug|Any CPU.Build.0 = Debug|Any CPU
{AF84FB43-ACBE-40DC-A75A-E4CCF01E8ADD}.Release|Any CPU.ActiveCfg = Release|Any CPU
{AF84FB43-ACBE-40DC-A75A-E4CCF01E8ADD}.Release|Any CPU.Build.0 = Release|Any CPU
{613CDE20-C71A-4EE1-97E8-226DAEC69C37}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{613CDE20-C71A-4EE1-97E8-226DAEC69C37}.Debug|Any CPU.Build.0 = Debug|Any CPU
{613CDE20-C71A-4EE1-97E8-226DAEC69C37}.Release|Any CPU.ActiveCfg = Release|Any CPU
{613CDE20-C71A-4EE1-97E8-226DAEC69C37}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {F7449170-D6DD-4743-AD41-F6E0963B21EE}
EndGlobalSection
EndGlobal

+ 3
- 0
Logging/.gitignore View File

@@ -0,0 +1,3 @@
.vs
bin
obj

+ 56
- 0
Logging/Logging.csproj View File

@@ -0,0 +1,56 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="12.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProjectGuid>{AF84FB43-ACBE-40DC-A75A-E4CCF01E8ADD}</ProjectGuid>
<OutputType>Library</OutputType>
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>logging</RootNamespace>
<AssemblyName>logger</AssemblyName>
<TargetFrameworkVersion>v4.6.1</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
<TargetFrameworkProfile />
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>bin\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<Prefer32Bit>false</Prefer32Bit>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<DebugType>pdbonly</DebugType>
<Optimize>true</Optimize>
<OutputPath>bin\Release\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<Prefer32Bit>false</Prefer32Bit>
</PropertyGroup>
<ItemGroup>
<Reference Include="System" />
<Reference Include="System.Core" />
<Reference Include="System.Xml.Linq" />
<Reference Include="System.Data.DataSetExtensions" />
<Reference Include="Microsoft.CSharp" />
<Reference Include="System.Data" />
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="logging.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets.
<Target Name="BeforeBuild">
</Target>
<Target Name="AfterBuild">
</Target>
-->
</Project>

+ 36
- 0
Logging/Properties/AssemblyInfo.cs View File

@@ -0,0 +1,36 @@
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;

// General Information about an assembly is controlled through the following
// set of attributes. Change these attribute values to modify the information
// associated with an assembly.
[assembly: AssemblyTitle("Logger")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("Logger")]
[assembly: AssemblyCopyright("Copyright © 2018")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]

// Setting ComVisible to false makes the types in this assembly not visible
// to COM components. If you need to access a type in this assembly from
// COM, set the ComVisible attribute to true on that type.
[assembly: ComVisible(false)]

// The following GUID is for the ID of the typelib if this project is exposed to COM
[assembly: Guid("76a427dd-d642-44e4-91a4-edb0b44132dc")]

// Version information for an assembly consists of the following four values:
//
// Major Version
// Minor Version
// Build Number
// Revision
//
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]

+ 182
- 0
Logging/logging.cs View File

@@ -0,0 +1,182 @@
using System;
using System.Linq;
using System.IO;
using System.Diagnostics;
using System.Threading;
using System.Globalization;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Reflection;
using System.Net.Sockets;
using System.Net;
using System.Text;

namespace logging
{
public enum Log_level
{
Trace, Warning, Uncaught, Error
}

public interface Logger
{
void log(String format);
string format(Log_level ll, string messsage);
}

public static class LogPoint
{

private static volatile Logger logger;

public static void initialize(Logger logger)
{
if (LogPoint.logger != null)
{
throw new ArgumentException("LogPoint can only be initialized once!");
}
LogPoint.logger = logger;

AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(uncaught_handler);
}

static void uncaught_handler(object sender, UnhandledExceptionEventArgs args)
{
Exception e = (Exception)args.ExceptionObject;

log(e, Log_level.Uncaught);

}
public static void log(Exception e, Log_level ll)
{
log(e.ToString(), ll);
}

public static void log(Exception e)
{
log(e.ToString(), Log_level.Error);
}

public static void log(string logtext)
{
log(logtext, Log_level.Trace);
}

public static void log(string logtext, Log_level ll)
{

if (logger != null)
{
string format = logger.format(ll, logtext);
Console.WriteLine(format);
Trace.WriteLine(format);

logger.log(format);
}
else
{
string format = ll + " - " + logtext;
Console.WriteLine(format);
Trace.WriteLine(format);
}
}

}

public class GelfLogger : Logger
{
private Socket sock = new Socket(AddressFamily.InterNetwork, SocketType.Dgram,
ProtocolType.Udp);

private readonly string program_name;
private readonly IPEndPoint endpoint;
public GelfLogger(IPEndPoint endpoint, string program_name)
{
this.endpoint = endpoint;
this.program_name = program_name;

}

public void log(string format)
{
sock.SendTo(Encoding.ASCII.GetBytes(format), endpoint);
}

public string format(Log_level ll, string messsage)
{
return $"{{ \"version\": \"1.1\", \"host\" : \"{Environment.MachineName}\",\"short_message\" : \"{messsage}\", \"user\" : \"{Environment.UserName}\", \"thread\" : \"{Thread.CurrentThread.ManagedThreadId}\", \"program_name\" : \"{program_name}\", \"log_level\" : \"{ll}\" }}"; ;
}
}

public class FileLogger : Logger
{
private readonly string log_path;

private BlockingCollection<string> write_queue = new BlockingCollection<string>();

public FileLogger(string log_path)
{
this.log_path = log_path.EndsWith("\\") ? log_path : log_path + "\\";
Directory.CreateDirectory(this.log_path);

// process log messages asynchronously
Thread t = new Thread(() =>
{
while (true)
{
string text = write_queue.Take();
write(text);
}

});

AppDomain.CurrentDomain.ProcessExit += (s, e) =>
{
string text;
while(write_queue.TryTake(out text))
{
write(text);
}
};

// needs to be set, so that the process doesnt just keep on running
t.IsBackground = true;
t.Start();
}
void write(string s)
{
lock (this)
{
StreamWriter SW;
SW = File.AppendText(Path.Combine(log_path, get_logfile_name()));
SW.WriteLine(s);
SW.Close();
}
}

public string format(Log_level ll, string text)
{
string log = $"Level : {ll}\r\n" +
$"TS : {timestamp_string()}\r\n" +
$"Thread : {Thread.CurrentThread.ManagedThreadId}\r\n" +
$"{text}\r\n";
return log;

}
private static string get_logfile_name()
{
return $"{Environment.UserName}@{Environment.MachineName}_{DateTime.Now.ToString("dd-MM-yy")}.log";
}
public void log(string format)
{
write_queue.Add(format);
}

private static String timestamp_string()
{
return DateTime.Now.ToLongTimeString() + " " +
DateTime.Now.ToLongDateString();
}
}

}

+ 3
- 0
T_logging/.gitignore View File

@@ -0,0 +1,3 @@
.vs
bin
obj

+ 20
- 0
T_logging/Properties/AssemblyInfo.cs View File

@@ -0,0 +1,20 @@
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;

[assembly: AssemblyTitle("T_logging")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("HP Inc.")]
[assembly: AssemblyProduct("T_logging")]
[assembly: AssemblyCopyright("Copyright © HP Inc. 2020")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]

[assembly: ComVisible(false)]

[assembly: Guid("613cde20-c71a-4ee1-97e8-226daec69c37")]

// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]

+ 45
- 0
T_logging/T_logging.cs View File

@@ -0,0 +1,45 @@
using System;
using System.Collections.Generic;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading;
using logging;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace T_logging
{
[TestClass]
public class T_logging
{
[TestMethod]
public void gelf()
{
Socket sock = new Socket(AddressFamily.InterNetwork, SocketType.Dgram,
ProtocolType.Udp);

var addr = Dns.GetHostAddresses("bloc")[0];
IPEndPoint endPoint = new IPEndPoint(addr, 12201);

string messg = $"{{ \"version\": \"1.1\", \"host\" : \"host\",\"short_message\" : \"heyo\" }}";

List<Thread> threads = new List<Thread>();

for (int i = 0; i< 100; i++)
{
Thread t = new Thread(() => sock.SendTo(Encoding.ASCII.GetBytes(messg), endPoint));
threads.Add(t);
}
foreach(var t in threads)
{
t.Start();
}
foreach (var t in threads)
{
t.Join();
}

}
}
}

+ 74
- 0
T_logging/T_logging.csproj View File

@@ -0,0 +1,74 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="15.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="..\packages\MSTest.TestAdapter.2.1.1\build\net45\MSTest.TestAdapter.props" Condition="Exists('..\packages\MSTest.TestAdapter.2.1.1\build\net45\MSTest.TestAdapter.props')" />
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProjectGuid>{613CDE20-C71A-4EE1-97E8-226DAEC69C37}</ProjectGuid>
<OutputType>Library</OutputType>
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>T_logging</RootNamespace>
<AssemblyName>T_logging</AssemblyName>
<TargetFrameworkVersion>v4.6.1</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
<ProjectTypeGuids>{3AC096D0-A1C2-E12C-1390-A8335801FDAB};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}</ProjectTypeGuids>
<VisualStudioVersion Condition="'$(VisualStudioVersion)' == ''">15.0</VisualStudioVersion>
<VSToolsPath Condition="'$(VSToolsPath)' == ''">$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)</VSToolsPath>
<ReferencePath>$(ProgramFiles)\Common Files\microsoft shared\VSTT\$(VisualStudioVersion)\UITestExtensionPackages</ReferencePath>
<IsCodedUITest>False</IsCodedUITest>
<TestProjectType>UnitTest</TestProjectType>
<NuGetPackageImportStamp>
</NuGetPackageImportStamp>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>bin\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<DebugType>pdbonly</DebugType>
<Optimize>true</Optimize>
<OutputPath>bin\Release\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<ItemGroup>
<Reference Include="Microsoft.VisualStudio.TestPlatform.TestFramework, Version=14.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
<HintPath>..\packages\MSTest.TestFramework.2.1.1\lib\net45\Microsoft.VisualStudio.TestPlatform.TestFramework.dll</HintPath>
</Reference>
<Reference Include="Microsoft.VisualStudio.TestPlatform.TestFramework.Extensions, Version=14.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
<HintPath>..\packages\MSTest.TestFramework.2.1.1\lib\net45\Microsoft.VisualStudio.TestPlatform.TestFramework.Extensions.dll</HintPath>
</Reference>
<Reference Include="System" />
<Reference Include="System.Core" />
</ItemGroup>
<ItemGroup>
<Compile Include="T_logging.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
<ItemGroup>
<None Include="packages.config" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\Logging\logging.csproj">
<Project>{af84fb43-acbe-40dc-a75a-e4ccf01e8add}</Project>
<Name>logging</Name>
</ProjectReference>
</ItemGroup>
<Import Project="$(VSToolsPath)\TeamTest\Microsoft.TestTools.targets" Condition="Exists('$(VSToolsPath)\TeamTest\Microsoft.TestTools.targets')" />
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<Target Name="EnsureNuGetPackageBuildImports" BeforeTargets="PrepareForBuild">
<PropertyGroup>
<ErrorText>Dieses Projekt verweist auf mindestens ein NuGet-Paket, das auf diesem Computer fehlt. Verwenden Sie die Wiederherstellung von NuGet-Paketen, um die fehlenden Dateien herunterzuladen. Weitere Informationen finden Sie unter "http://go.microsoft.com/fwlink/?LinkID=322105". Die fehlende Datei ist "{0}".</ErrorText>
</PropertyGroup>
<Error Condition="!Exists('..\packages\MSTest.TestAdapter.2.1.1\build\net45\MSTest.TestAdapter.props')" Text="$([System.String]::Format('$(ErrorText)', '..\packages\MSTest.TestAdapter.2.1.1\build\net45\MSTest.TestAdapter.props'))" />
<Error Condition="!Exists('..\packages\MSTest.TestAdapter.2.1.1\build\net45\MSTest.TestAdapter.targets')" Text="$([System.String]::Format('$(ErrorText)', '..\packages\MSTest.TestAdapter.2.1.1\build\net45\MSTest.TestAdapter.targets'))" />
</Target>
<Import Project="..\packages\MSTest.TestAdapter.2.1.1\build\net45\MSTest.TestAdapter.targets" Condition="Exists('..\packages\MSTest.TestAdapter.2.1.1\build\net45\MSTest.TestAdapter.targets')" />
</Project>

+ 5
- 0
T_logging/packages.config View File

@@ -0,0 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="MSTest.TestAdapter" version="2.1.1" targetFramework="net461" />
<package id="MSTest.TestFramework" version="2.1.1" targetFramework="net461" />
</packages>

Loading…
Cancel
Save