From eea7d58f1284689b7b5df9b32686b72cf1cf2ae7 Mon Sep 17 00:00:00 2001 From: gaufung Date: Thu, 14 Sep 2023 20:29:45 +0800 Subject: [PATCH] #7 Using StringIsNullOrWhiltespace method --- Src/Agent.cs | 2 +- Src/Spec.cs | 27 +++++++++++---------------- 2 files changed, 12 insertions(+), 17 deletions(-) diff --git a/Src/Agent.cs b/Src/Agent.cs index bfafdbb..62b5473 100644 --- a/Src/Agent.cs +++ b/Src/Agent.cs @@ -59,7 +59,7 @@ public static void RegisterFromYaml(String yamlFile) } TraceManager.Trace128Bits = spec.Id128bit; IZipkinSender sender = default(IZipkinSender); - if (spec.OutputServerUrl == null || spec.OutputServerUrl.Trim().Equals("")) + if (string.IsNullOrWhiteSpace(spec.OutputServerUrl)) { LoggerManager.GetTracingLogger().LogInformation("out put server was empty, use console reporter for trace."); sender = new ConsoleSender(); diff --git a/Src/Spec.cs b/Src/Spec.cs index 6c3554a..be87af7 100644 --- a/Src/Spec.cs +++ b/Src/Spec.cs @@ -83,34 +83,34 @@ public static Spec Load(string yamlFile) switch (kvp.Key) { case "serviceName": - spec.ServiceName = (string)(isEmpty(kvp.Value) ? "" : kvp.Value); + spec.ServiceName = string.IsNullOrWhiteSpace(kvp.Value) ? string.Empty : kvp.Value; break; case "tracing.type": - spec.TracingType = (string)(isEmpty(kvp.Value) ? "" : kvp.Value); + spec.TracingType = string.IsNullOrWhiteSpace(kvp.Value) ? string.Empty : kvp.Value; break; case "tracing.enable": - spec.TracingEnable = isEmpty(kvp.Value) ? true : bool.Parse(kvp.Value); + spec.TracingEnable = string.IsNullOrWhiteSpace(kvp.Value) ? true : bool.Parse(kvp.Value); break; case "tracing.sample.rate": - spec.SampleRate = isEmpty(kvp.Value) ? 1.0F : float.Parse(kvp.Value); + spec.SampleRate = string.IsNullOrWhiteSpace(kvp.Value) ? 1.0F : float.Parse(kvp.Value); break; case "tracing.shared.spans": - spec.SharedSpans = isEmpty(kvp.Value) ? true : bool.Parse(kvp.Value); + spec.SharedSpans = string.IsNullOrWhiteSpace(kvp.Value) ? true : bool.Parse(kvp.Value); break; case "tracing.id128bit": - spec.Id128bit = isEmpty(kvp.Value) ? false : bool.Parse(kvp.Value); + spec.Id128bit = string.IsNullOrWhiteSpace(kvp.Value) ? false : bool.Parse(kvp.Value); break; case "reporter.output.server": - spec.OutputServerUrl = isEmpty(kvp.Value) ? "" : kvp.Value; + spec.OutputServerUrl = string.IsNullOrWhiteSpace(kvp.Value) ? string.Empty : kvp.Value; break; case "reporter.output.server.tls.enable": - spec.EnableTls = isEmpty(kvp.Value) ? false : bool.Parse(kvp.Value); + spec.EnableTls = string.IsNullOrWhiteSpace(kvp.Value) ? false : bool.Parse(kvp.Value); break; case "reporter.output.server.tls.key": - spec.TlsKey = isEmpty(kvp.Value) ? "" : kvp.Value; + spec.TlsKey = string.IsNullOrWhiteSpace(kvp.Value) ? string.Empty : kvp.Value; break; case "reporter.output.server.tls.cert": - spec.TlsCert = isEmpty(kvp.Value) ? "" : kvp.Value; + spec.TlsCert = string.IsNullOrWhiteSpace(kvp.Value) ? string.Empty : kvp.Value; break; default: break; @@ -119,14 +119,9 @@ public static Spec Load(string yamlFile) return spec; } - public static bool isEmpty(string v) - { - return v == null || v.Length == 0 || v.Trim().Length == 0; - } - public void Validate() { - if (EnableTls && (isEmpty(TlsKey) || isEmpty(TlsCert))) + if (EnableTls && (string.IsNullOrWhiteSpace(TlsKey) || string.IsNullOrWhiteSpace(TlsCert))) { throw new FormatException("key, cert are not all specified"); }