Ejemplo n.º 1
0
 public async Task <WebRet> GetAsync(string url, CancellationToken cancel = default)
 {
     return(await TaskUtil.RetryAsync(async() =>
     {
         return await Api.SimpleQueryAsync(WebMethods.GET, url, cancel);
     },
                                      this.Options.RetryIntervalMsecs,
                                      this.Options.TryCount,
                                      cancel,
                                      this.Options.RandomInterval));
 }
Ejemplo n.º 2
0
    public override async Task FlushAsync(bool halfFlush = false, CancellationToken cancel = default)
    {
        List <string> linesToSend;
        List <string> lines2ToSend;

        try
        {
            if (MyLocalIp == null)
            {
                MyLocalIp = await GetMyPrivateIpNativeUtil.GetMyPrivateIpAsync(IPVersion.IPv4);
            }
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex.ToString());
        }

        if (MyIpInfo == null)
        {
            try
            {
                await using GetMyIpClient c = new GetMyIpClient();

                MyIpInfo = await c.GetMyIpInfoAsync(IPVersion.IPv4, cancel);
            }
            catch
            {
            }
        }

        string globalInfo = "(Unknown)";

        if (MyIpInfo != null)
        {
            if (MyIpInfo.GlobalFqdn._IsSamei(MyIpInfo.GlobalIpAddress.ToString()))
            {
                globalInfo = MyIpInfo.GlobalFqdn;
            }
            else
            {
                globalInfo = $"{MyIpInfo.GlobalFqdn} - {MyIpInfo.GlobalIpAddress}";
            }
        }

        lock (this.Lock)
        {
            if (this.Lines.Count == 0 && this.Lines2.Count == 0)
            {
                return;
            }

            linesToSend  = this.Lines;
            lines2ToSend = this.Lines2;

            this.Lines  = new List <string>();
            this.Lines2 = new List <string>();
        }

        string cmdName = CoresLib.Report_CommandName;

        if (cmdName._IsEmpty())
        {
            cmdName = "Unknown";
        }

        string resultStr = CoresLib.Report_SimpleResult._OneLine();

        if (resultStr._IsEmpty())
        {
            resultStr = "Ok";
        }

        StringWriter w = new StringWriter();

        w.WriteLine($"Reported: {DtOffsetNow._ToDtStr()}");
        w.WriteLine($"Program: {CoresLib.AppName} Built: {Env.BuildTimeStamp._ToDtStr()}");
        w.WriteLine($"Hostname: {Env.DnsFqdnHostName}");
        w.WriteLine($"Global: {globalInfo}, Local: {MyLocalIp}");
        w.WriteLine($"Command: {cmdName}, Result: {(CoresLib.Report_HasError ? "*Error* - " : "OK - ")}{resultStr}");

        if (lines2ToSend.Count >= 1)
        {
            w.WriteLine("=====================");
            w.WriteLine();

            lines2ToSend.ForEach(x => w.WriteLine(x));

            w.WriteLine();
        }

        if (linesToSend.Count >= 1)
        {
            w.WriteLine("--------------------");
            w.WriteLine();

            linesToSend.ForEach(x => w.WriteLine(x));

            w.WriteLine();
        }

        w.WriteLine("--------------------");
        EnvInfoSnapshot snapshot = new EnvInfoSnapshot();

        snapshot.CommandLine = "";
        w.WriteLine($"Program Details: {snapshot._GetObjectDump()}");

        w.WriteLine();

        string subject = $"Report - {cmdName}{(CoresLib.Report_HasError ? " *Error*" : "")} - {linesToSend.Count} lines - {Env.DnsHostName} - {MyLocalIp} ({globalInfo}): {resultStr._NormalizeSoftEther(true)._TruncStrEx(60)}";

        var        hostAndPort = this.Settings.SmtpServer._ParseHostnaneAndPort(Consts.Ports.Smtp);
        SmtpConfig cfg         = new SmtpConfig(hostAndPort.Item1, hostAndPort.Item2, this.Settings.SmtpUseSsl, this.Settings.SmtpUsername, this.Settings.SmtpPassword);

        Console.WriteLine("Report Subject: " + subject);

        try
        {
            Console.WriteLine($"SMTP Log Sending to '{this.Settings.MailTo}' ...");

            await TaskUtil.RetryAsync(async() =>
            {
                await SmtpUtil.SendAsync(cfg, this.Settings.MailFrom, this.Settings.MailTo, subject, w.ToString(), false, cancel);

                return(0);
            }, 1000, 3, cancel : cancel, randomInterval : true);

            Console.WriteLine("SMTP Log Sent.");
        }
        catch (Exception ex)
        {
            Console.WriteLine("SMTP Log Send Error: " + ex.ToString());
        }
    }