From 4c7b9fd38ad614dbf37c37e45569790b44868458 Mon Sep 17 00:00:00 2001 From: Jens Geyer Date: Sat, 4 Dec 2021 22:48:37 +0100 Subject: THRIFT-5479 Add net 6 support Client: netstd Patch: Jens Geyer --- tutorial/netstd/Client/Client.csproj | 4 +- tutorial/netstd/Client/Program.cs | 87 ++++++++++++++-------------- tutorial/netstd/Interfaces/Interfaces.csproj | 4 +- tutorial/netstd/Server/Program.cs | 85 ++++++++++++++------------- tutorial/netstd/Server/Server.csproj | 4 +- 5 files changed, 93 insertions(+), 91 deletions(-) (limited to 'tutorial') diff --git a/tutorial/netstd/Client/Client.csproj b/tutorial/netstd/Client/Client.csproj index 1f877517e..cf01951be 100644 --- a/tutorial/netstd/Client/Client.csproj +++ b/tutorial/netstd/Client/Client.csproj @@ -19,7 +19,7 @@ --> - net5.0 + net6.0 9.0 Client Client @@ -32,7 +32,7 @@ - + diff --git a/tutorial/netstd/Client/Program.cs b/tutorial/netstd/Client/Program.cs index 9c47450e7..f1c5236e1 100644 --- a/tutorial/netstd/Client/Program.cs +++ b/tutorial/netstd/Client/Program.cs @@ -39,10 +39,25 @@ using shared; namespace Client { + public static class LoggingHelper + { + public static ILoggerFactory LogFactory { get; } = LoggerFactory.Create(builder => { + ConfigureLogging(builder); + }); + + public static void ConfigureLogging(ILoggingBuilder logging) + { + logging.SetMinimumLevel(LogLevel.Trace); + logging.AddConsole(); + logging.AddDebug(); + } + + public static ILogger CreateLogger() => LogFactory.CreateLogger(); + } + public class Program { - private static readonly ServiceCollection ServiceCollection = new(); - private static ILogger Logger; + private static readonly ILogger Logger = LoggingHelper.CreateLogger(); private static readonly TConfiguration Configuration = null; // new TConfiguration() if needed private static void DisplayHelp() @@ -86,47 +101,35 @@ Sample: { args ??= Array.Empty(); - ServiceCollection.AddLogging(logging => ConfigureLogging(logging)); - using (var serviceProvider = ServiceCollection.BuildServiceProvider()) + if (args.Any(x => x.StartsWith("-help", StringComparison.OrdinalIgnoreCase))) { - Logger = serviceProvider.GetService().CreateLogger(nameof(Client)); - - if (args.Any(x => x.StartsWith("-help", StringComparison.OrdinalIgnoreCase))) - { - DisplayHelp(); - return; - } + DisplayHelp(); + return; + } - Logger.LogInformation("Starting client..."); + Logger.LogInformation("Starting client..."); - using (var source = new CancellationTokenSource()) - { - RunAsync(args, source.Token).GetAwaiter().GetResult(); - } + using (var source = new CancellationTokenSource()) + { + RunAsync(args, source.Token).GetAwaiter().GetResult(); } } - private static void ConfigureLogging(ILoggingBuilder logging) - { - logging.SetMinimumLevel(LogLevel.Trace); - logging.AddConsole(); - logging.AddDebug(); - } - + private static async Task RunAsync(string[] args, CancellationToken cancellationToken) { var numClients = GetNumberOfClients(args); - Logger.LogInformation($"Selected # of clients: {numClients}"); + Logger.LogInformation("Selected # of clients: {numClients}", numClients); var transport = GetTransport(args); - Logger.LogInformation($"Selected client transport: {transport}"); + Logger.LogInformation("Selected client transport: {transport}", transport); var protocol = MakeProtocol( args, MakeTransport(args)); - Logger.LogInformation($"Selected client protocol: {GetProtocol(args)}"); + Logger.LogInformation("Selected client protocol: {GetProtocol(args)}", GetProtocol(args)); var mplex = GetMultiplex(args); - Logger.LogInformation("Multiplex " + (mplex ? "yes" : "no")); + Logger.LogInformation("Multiplex {mplex}", mplex); var tasks = new Task[numClients]; for (int i = 0; i < numClients; i++) @@ -240,7 +243,7 @@ Sample: { var numClients = args.FirstOrDefault(x => x.StartsWith("-mc"))?.Split(':')?[1]; - Logger.LogInformation($"Selected # of clients: {numClients}"); + Logger.LogInformation("Selected # of clients: {numClients}", numClients); if (int.TryParse(numClients, out int c) && (0 < c) && (c <= 100)) return c; @@ -310,7 +313,7 @@ Sample: } catch (Exception ex) { - Logger.LogError(ex.ToString()); + Logger.LogError("{ex}",ex); } finally { @@ -319,7 +322,7 @@ Sample: } catch (TApplicationException x) { - Logger.LogError(x.ToString()); + Logger.LogError("{x}",x); } } @@ -329,12 +332,12 @@ Sample: // Async version - Logger.LogInformation($"{client.ClientId} Ping()"); + Logger.LogInformation("{client.ClientId} Ping()", client.ClientId); await client.ping(cancellationToken); - Logger.LogInformation($"{client.ClientId} Add(1,1)"); + Logger.LogInformation("{client.ClientId} Add(1,1)", client.ClientId); var sum = await client.add(1, 1, cancellationToken); - Logger.LogInformation($"{client.ClientId} Add(1,1)={sum}"); + Logger.LogInformation("{client.ClientId} Add(1,1)={sum}", client.ClientId, sum); var work = new Work { @@ -345,13 +348,13 @@ Sample: try { - Logger.LogInformation($"{client.ClientId} Calculate(1)"); + Logger.LogInformation("{client.ClientId} Calculate(1)", client.ClientId); await client.calculate(1, work, cancellationToken); - Logger.LogInformation($"{client.ClientId} Whoa we can divide by 0"); + Logger.LogInformation("{client.ClientId} Whoa we can divide by 0", client.ClientId); } catch (InvalidOperation io) { - Logger.LogInformation($"{client.ClientId} Invalid operation: " + io); + Logger.LogInformation("{client.ClientId} Invalid operation: {io}", client.ClientId, io); } work.Op = Operation.SUBTRACT; @@ -360,20 +363,20 @@ Sample: try { - Logger.LogInformation($"{client.ClientId} Calculate(1)"); + Logger.LogInformation("{client.ClientId} Calculate(1)", client.ClientId); var diff = await client.calculate(1, work, cancellationToken); - Logger.LogInformation($"{client.ClientId} 15-10={diff}"); + Logger.LogInformation("{client.ClientId} 15-10={diff}", client.ClientId, diff); } catch (InvalidOperation io) { - Logger.LogInformation($"{client.ClientId} Invalid operation: " + io); + Logger.LogInformation("{client.ClientId} Invalid operation: {io}", client.ClientId, io); } - Logger.LogInformation($"{client.ClientId} GetStruct(1)"); + Logger.LogInformation("{client.ClientId} GetStruct(1)", client.ClientId); var log = await client.getStruct(1, cancellationToken); - Logger.LogInformation($"{client.ClientId} Check log: {log.Value}"); + Logger.LogInformation("{client.ClientId} Check log: {log.Value}", client.ClientId, log.Value); - Logger.LogInformation($"{client.ClientId} Zip() with delay 100mc on server side"); + Logger.LogInformation("{client.ClientId} Zip() with delay 100mc on server side", client.ClientId); await client.zip(cancellationToken); } diff --git a/tutorial/netstd/Interfaces/Interfaces.csproj b/tutorial/netstd/Interfaces/Interfaces.csproj index 989f6db40..a28874585 100644 --- a/tutorial/netstd/Interfaces/Interfaces.csproj +++ b/tutorial/netstd/Interfaces/Interfaces.csproj @@ -19,7 +19,7 @@ --> - net5.0 + net6.0 Interfaces Interfaces 0.16.0.0 @@ -34,7 +34,7 @@ - + diff --git a/tutorial/netstd/Server/Program.cs b/tutorial/netstd/Server/Program.cs index 80205d54f..d9c9dbf12 100644 --- a/tutorial/netstd/Server/Program.cs +++ b/tutorial/netstd/Server/Program.cs @@ -38,53 +38,55 @@ using shared; using Thrift.Processor; using System.Diagnostics; -#pragma warning disable IDE0063 // using #pragma warning disable IDE0057 // substr namespace Server { + public static class LoggingHelper + { + public static ILoggerFactory LogFactory { get; } = LoggerFactory.Create(builder => { + ConfigureLogging(builder); + }); + + public static void ConfigureLogging(ILoggingBuilder logging) + { + logging.SetMinimumLevel(LogLevel.Trace); + logging.AddConsole(); + logging.AddDebug(); + } + + public static ILogger CreateLogger() => LogFactory.CreateLogger(); + } + public class Program { - private static readonly ServiceCollection ServiceCollection = new(); - private static ILogger Logger; + private static readonly ILogger Logger = LoggingHelper.CreateLogger(); private static readonly TConfiguration Configuration = null; // new TConfiguration() if needed public static void Main(string[] args) { args ??= Array.Empty(); - ServiceCollection.AddLogging(logging => ConfigureLogging(logging)); - using (var serviceProvider = ServiceCollection.BuildServiceProvider()) + if (args.Any(x => x.StartsWith("-help", StringComparison.OrdinalIgnoreCase))) { - Logger = serviceProvider.GetService().CreateLogger(nameof(Server)); - - if (args.Any(x => x.StartsWith("-help", StringComparison.OrdinalIgnoreCase))) - { - DisplayHelp(); - return; - } - - using (var source = new CancellationTokenSource()) - { - RunAsync(args, source.Token).GetAwaiter().GetResult(); + DisplayHelp(); + return; + } - Logger.LogInformation("Press any key to stop..."); + using (var source = new CancellationTokenSource()) + { + RunAsync(args, source.Token).GetAwaiter().GetResult(); - Console.ReadLine(); - source.Cancel(); - } + Logger.LogInformation("Press any key to stop..."); - Logger.LogInformation("Server stopped"); + Console.ReadLine(); + source.Cancel(); } - } - private static void ConfigureLogging(ILoggingBuilder logging) - { - logging.SetMinimumLevel(LogLevel.Trace); - logging.AddConsole(); - logging.AddDebug(); + Logger.LogInformation("Server stopped"); } + private static void DisplayHelp() { Logger.LogInformation(@" @@ -226,15 +228,12 @@ Sample: try { Logger.LogInformation( - string.Format( - "TSimpleAsyncServer with \n{0} transport\n{1} buffering\nmultiplex = {2}\n{3} protocol", - transport, - buffering, - multiplex ? "yes" : "no", - protocol - )); - - var loggerFactory = ServiceCollection.BuildServiceProvider().GetService(); + "TSimpleAsyncServer with \n{transport} transport\n{buffering} buffering\nmultiplex = {multiplex}\n{protocol} protocol", + transport, + buffering, + multiplex ? "yes" : "no", + protocol + ); var server = new TSimpleAsyncServer( itProcessorFactory: new TSingletonProcessorFactory(processor), @@ -243,7 +242,7 @@ Sample: outputTransportFactory: transportFactory, inputProtocolFactory: protocolFactory, outputProtocolFactory: protocolFactory, - logger: loggerFactory.CreateLogger()); + logger: LoggingHelper.CreateLogger()); Logger.LogInformation("Starting the server..."); @@ -251,7 +250,7 @@ Sample: } catch (Exception x) { - Logger.LogInformation(x.ToString()); + Logger.LogInformation("{x}",x); } } @@ -327,7 +326,7 @@ Sample: .UseUrls("http://localhost:9090") .UseContentRoot(Directory.GetCurrentDirectory()) .UseStartup() - .ConfigureLogging((ctx,logging) => ConfigureLogging(logging)) + .ConfigureLogging((ctx,logging) => LoggingHelper.ConfigureLogging(logging)) .Build(); Logger.LogTrace("test"); @@ -379,7 +378,7 @@ Sample: public async Task getStruct(int key, CancellationToken cancellationToken) { - Logger.LogInformation("GetStruct({0})", key); + Logger.LogInformation("GetStruct({key})", key); return await Task.FromResult(_log[key]); } @@ -391,13 +390,13 @@ Sample: public async Task add(int num1, int num2, CancellationToken cancellationToken) { - Logger.LogInformation($"Add({num1},{num2})"); + Logger.LogInformation("Add({num1},{num2})", num1, num2); return await Task.FromResult(num1 + num2); } public async Task calculate(int logid, Work w, CancellationToken cancellationToken) { - Logger.LogInformation($"Calculate({logid}, [{w.Op},{w.Num1},{w.Num2}])"); + Logger.LogInformation("Calculate({logid}, [{w.Op},{w.Num1},{w.Num2}])", logid, w.Op, w.Num1, w.Num2); int val; switch (w.Op) @@ -462,7 +461,7 @@ Sample: { public async Task getStruct(int key, CancellationToken cancellationToken) { - Logger.LogInformation("GetStruct({0})", key); + Logger.LogInformation("GetStruct({key})", key); return await Task.FromResult(new SharedStruct() { Key = key, diff --git a/tutorial/netstd/Server/Server.csproj b/tutorial/netstd/Server/Server.csproj index e5e54a5b1..6bee63fc3 100644 --- a/tutorial/netstd/Server/Server.csproj +++ b/tutorial/netstd/Server/Server.csproj @@ -19,7 +19,7 @@ --> - net5.0 + net6.0 9.0 Server Server @@ -40,6 +40,6 @@ - + -- cgit v1.2.1