コード例 #1
0
        public void ErrorHandler(object sender, ErrorInformation errorInformation)
        {
            var cmdlet = _cmdlet;

            if (cmdlet == null)
            {
                return;
            }

            var exceptionName = errorInformation.Exception.InnerException?.GetType().Name ?? errorInformation.Exception.GetType().Name;

            var errorMessage = $"Perry ({exceptionName}): {errorInformation.ErrorMessage}";

            if (_options?.IncludeVariable == true)
            {
                var variables = VariableParser.GetVariables(errorInformation.ErrorMessage);
                foreach (var variable in variables)
                {
                    var variableValue = cmdlet.GetVariableValue(variable).ToString();
                    errorMessage += $"{Environment.NewLine}   Variable: ${variable} = {variableValue}";
                }
            }

            if (_options?.IncludeException == true)
            {
                errorMessage += $"{Environment.NewLine}{errorInformation.Exception}";
            }

            cmdlet.WriteWarning(errorMessage);
        }
コード例 #2
0
            public void GetNoVariable()
            {
                var inputData = "Cause-Error -Input 'Oops'";

                var result = VariableParser.GetVariables(inputData);

                Assert.Empty(result);
            }
コード例 #3
0
            public void GetMultipleVariables()
            {
                var inputData = "$var1 / $var2";

                var result = VariableParser.GetVariables(inputData);

                Assert.Equal(new[] { "var1", "var2" }, result);
            }
コード例 #4
0
            public void GetVariableWithoutNamespace()
            {
                var inputData = "10 / $myvariable";

                var result = VariableParser.GetVariables(inputData).Single();

                Assert.Equal("myvariable", result);
            }
コード例 #5
0
        public void ErrorHandler(object sender, ErrorInformation errorInformation)
        {
            var cmdlet = _cmdlet;

            if (cmdlet == null || _options == null)
            {
                return;
            }

            var exceptionName = errorInformation.Exception.InnerException?.GetType().Name ?? errorInformation.Exception.GetType().FullName;

            var errorMessage = $"{DateTime.UtcNow:u} {exceptionName}{Environment.NewLine}{errorInformation.ErrorMessage}";

            if (_options?.IncludeVariable == true)
            {
                var variables = VariableParser.GetVariables(errorInformation.ErrorMessage);
                foreach (var variable in variables)
                {
                    var variableValue = cmdlet.GetVariableValue(variable).ToString();
                    errorMessage += $"{Environment.NewLine}   Variable: ${variable} = {variableValue}";
                }
            }

            if (_options?.IncludeException == true)
            {
                errorMessage += $"{Environment.NewLine}{errorInformation.Exception}";
            }

            var outputFile = Path.Combine(_options.LogPath, "perry.log");

            lock (_syncLogs)
            {
                try
                {
                    RollLogFiles(outputFile);
                    File.AppendAllText(outputFile, $"{errorMessage}{Environment.NewLine}", Encoding.UTF8);
                }
                catch (Exception e)
                {
                    Trace.TraceError(e.ToString());
                }
            }
        }