Skip to content

Commit

Permalink
Initial program
Browse files Browse the repository at this point in the history
  • Loading branch information
Nephiaust committed Sep 7, 2023
1 parent 9cd4bc0 commit e8bec21
Show file tree
Hide file tree
Showing 5 changed files with 149 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
.idea
/uploads/*
*/logs/*
/depreciated/*

# General Apple files
Expand Down
87 changes: 87 additions & 0 deletions SMTP/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
// See https://aka.ms/new-console-template for more information

using Serilog;
using Serilog.Core;
using Serilog.Events;
using System;
using System.IO;
using System.Net;
using System.Net.Sockets;
using System.Text;

namespace SMTPServer
{
public class MyGlobalVariables
{
public static bool KeepRunning { get; set; }
public AutoResetEvent connectionWaitHandle = new AutoResetEvent(false);
}

class Program
{
static void Main()
{
// Create the Serilog object
Log.Logger = new LoggerConfiguration()
.Enrich.With(new ThreadIdEnricher())
.Enrich.WithProperty("Version", "0.0.1")
//.ReadFrom.AppSettings()
.MinimumLevel.Verbose()
.WriteTo.Console(
outputTemplate: "{Timestamp:yyyy-MM-dd HH:mm:ss.fff zzz} [{Level:u3}, TID:{ThreadId}] {Message:lj}{NewLine}{Exception}")
.WriteTo.File("log.txt",
rollingInterval: RollingInterval.Day,
rollOnFileSizeLimit: true,
outputTemplate: "{Timestamp:yyyy-MM-dd HH:mm:ss.fff zzz} [{Level:u3}, TID:{ThreadId}] {Message:lj}{NewLine}{Exception}")
.CreateLogger();
Log.Verbose("Starting application");

// Create the TCP listening object
int port = 2525;
System.Net.IPAddress ListenIPAddress = System.Net.IPAddress.Any;
var MyTCPlistener = new TcpListener(ListenIPAddress, port);
MyTCPlistener.Start();

MyGlobalVariables.KeepRunning = true;

while (MyGlobalVariables.KeepRunning)
{
IAsyncResult result = MyTCPlistener.BeginAcceptTcpClient(SmtpThreadWork.AcceptConnection, MyTCPlistener);
MyGlobalVariables.connectionWaitHandle.WaitOne(); // Wait until a client has begun handling an event
MyGlobalVariables.connectionWaitHandle.Reset(); // Reset wait handle or the loop goes as fast as it can (after first request)
}

Log.CloseAndFlush();
}
class ThreadIdEnricher : ILogEventEnricher
{
public void Enrich(LogEvent logEvent, ILogEventPropertyFactory propertyFactory)
{
logEvent.AddPropertyIfAbsent(propertyFactory.CreateProperty(
"ThreadId", Thread.CurrentThread.ManagedThreadId));
}
}
}
public class SmtpThreadWork
{
public static void AcceptConnection(IAsyncResult NewConnection)
{
Log.Debug("Starting a new thread");
TcpListener listener = (TcpListener)NewConnection.AsyncState;
TcpClient client = listener.EndAcceptTcpClient(NewConnection);


}
}
public class ThreadWork
{
public static void DoWork()
{
for (int i = 0; i < 3; i++)
{
Log.Information("Working thread..." + i);
Thread.Sleep(100);
}
}
}
}
28 changes: 28 additions & 0 deletions SMTP/SMTP.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Serilog" Version="3.0.1" />
<PackageReference Include="Serilog.Enrichers.AssemblyName" Version="1.0.9" />
<PackageReference Include="Serilog.Enrichers.ClientInfo" Version="2.0.1" />
<PackageReference Include="Serilog.Enrichers.Context" Version="4.6.5" />
<PackageReference Include="Serilog.Enrichers.CorrelationId" Version="3.0.1" />
<PackageReference Include="Serilog.Enrichers.Environment" Version="2.2.0" />
<PackageReference Include="Serilog.Enrichers.Process" Version="2.0.2" />
<PackageReference Include="Serilog.Enrichers.Thread" Version="3.1.0" />
<PackageReference Include="Serilog.Formatting.Compact" Version="1.1.0" />
<PackageReference Include="Serilog.Settings.AppSettings" Version="2.2.2" />
<PackageReference Include="Serilog.Settings.Configuration" Version="7.0.1" />
<PackageReference Include="Serilog.Sinks.Console" Version="4.1.0" />
<PackageReference Include="Serilog.Sinks.Debug" Version="2.0.0" />
<PackageReference Include="Serilog.Sinks.EventLog" Version="3.1.0" />
<PackageReference Include="Serilog.Sinks.File" Version="5.0.0" />
</ItemGroup>

</Project>
8 changes: 8 additions & 0 deletions SMTP/program.config
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<appSettings>
<add key="serilog:write-to:Console" />
<add key="serilog:write-to:File" />
<add key="serilog:minimum-level" value="Verbose" />
</appSettings>
</configuration>
25 changes: 25 additions & 0 deletions dotMailServer.sln
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.7.34024.191
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SMTP", "SMTP\SMTP.csproj", "{A91B3684-0F04-4A7A-AB29-6B287466B47A}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{A91B3684-0F04-4A7A-AB29-6B287466B47A}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{A91B3684-0F04-4A7A-AB29-6B287466B47A}.Debug|Any CPU.Build.0 = Debug|Any CPU
{A91B3684-0F04-4A7A-AB29-6B287466B47A}.Release|Any CPU.ActiveCfg = Release|Any CPU
{A91B3684-0F04-4A7A-AB29-6B287466B47A}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {6EF1E636-0628-47B2-81A6-6488ED09654C}
EndGlobalSection
EndGlobal

0 comments on commit e8bec21

Please sign in to comment.