/******************************************************************************* * Copyright 2012-2018 Amazon.com, Inc. or its affiliates. All Rights Reserved. * Licensed under the Apache License, Version 2.0 (the "License"). You may not use * this file except in compliance with the License. A copy of the License is located at * * http://aws.amazon.com/apache2.0 * * or in the "license" file accompanying this file. * This file is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR * CONDITIONS OF ANY KIND, either express or implied. See the License for the * specific language governing permissions and limitations under the License. * ***************************************************************************** * * AWS Tools for Windows (TM) PowerShell (TM) * */ using System; using System.Collections.Generic; using System.Linq; using System.Text; using Microsoft.PowerShell.Commands; using Amazon; using System.Management.Automation; using System.Diagnostics; using System.Globalization; namespace Amazon.PowerShell.Common { /// /// Adds a single trace listener to the specified trace source. Given a name and file path, /// creates a TextWriterTraceListener with the given name and file path, and adds it to the /// listeners for the trace source. /// If Source is not specified, 'Amazon' is assumed, which represents all SDK API calls. /// In the case where there are multiple listeners for multiple sources, Trace calls for an /// API will go to the most specific source only. For example, if one listener is added to 'Amazon.S3' and /// another on 'Amazon', then S3 calls will only be logged to the former listener. /// [Cmdlet("Add", "AWSLoggingListener", DefaultParameterSetName = ParamSet_SimpleListener)] [OutputType("None")] [AWSCmdlet("Adds a listener to aws service calls and enable logging.")] [AWSCmdletOutput("None", "This cmdlet does not produce any output.")] public class AddLoggerCmdlet : PSCmdlet { const string ParamSet_SimpleListener = "SimpleListener"; const string ParamSet_CustomListener = "CustomListener"; /// /// The name of the logger. /// [Parameter(Position=0, Mandatory=true, ParameterSetName = ParamSet_SimpleListener, ValueFromPipelineByPropertyName = true)] public string Name { get; set; } /// /// File path to write the log to. /// [Parameter(Position=1, Mandatory=true, ParameterSetName = ParamSet_SimpleListener, ValueFromPipelineByPropertyName = true)] public string LogFilePath { get; set; } /// /// Specify a custom trace listener object. /// [Parameter(Position = 0, Mandatory = true, ParameterSetName = ParamSet_CustomListener, ValueFromPipelineByPropertyName = true)] public TraceListener TraceListener { get; set; } /// /// Specify a source to log responses for. /// /// Defaults to all responses (i.e. 'Amazon'). To limit to a specific service (for example DynamoDB), use 'Amazon.DynamoDB'.) /// /// [Parameter(ValueFromPipelineByPropertyName = true)] public string Source { get; set; } protected override void ProcessRecord() { var listener = this.ParameterSetName.Equals(ParamSet_SimpleListener, StringComparison.OrdinalIgnoreCase) ? new TextWriterTraceListener(LogFilePath, Name) : TraceListener; AWSConfigs.AddTraceListener(Source ?? "Amazon", listener); AWSConfigs.LoggingConfig.LogTo |= LoggingOptions.SystemDiagnostics; Trace.AutoFlush = true; // bump the logging level up to OnError at least. Might already be configured higher. if (AWSConfigs.LoggingConfig.LogResponses == ResponseLoggingOption.Never) AWSConfigs.LoggingConfig.LogResponses = ResponseLoggingOption.OnError; } } /// /// Remove a listener from and AWS API trace source. /// [Cmdlet("Remove", "AWSLoggingListener")] [OutputType("None")] [AWSCmdlet("Removes a logger from the specified source (e.g. 'Amazon', or 'Amazon.S3') by name.")] [AWSCmdletOutput("None", "This cmdlet does not produce any output.")] public class RemoveLoggerCmdlet : PSCmdlet { /// /// Source to remove the listener from. /// /// Examples: 'Amazon', or 'Amazon.DynamoDB'. /// /// [Parameter(Position=0, Mandatory=true, ValueFromPipelineByPropertyName = true)] public string Source { get; set; } /// /// Name of the trace listener to remove. /// [Parameter(Position=1, Mandatory=true, ValueFromPipelineByPropertyName = true)] public string Name { get; set; } protected override void ProcessRecord() { AWSConfigs.RemoveTraceListener(Source, Name); } } /// /// Modify when to produce log entries. /// [Cmdlet("Set", "AWSResponseLogging")] [OutputType("None")] [AWSCmdlet("Modify response logging options for AWS service requests.")] [AWSCmdletOutput("None", "This cmdlet does not produce any output.")] public class SetResponseLoggingCmdlet : PSCmdlet { const string ResponseLogging_All = "Always", ResponseLogging_Error = "OnError", ResponseLogging_Never = "Never"; /// /// When to log responses. /// /// /// Must be one of 'Always', 'OnError', or 'Never'. /// [Parameter(Position=0, ValueFromPipelineByPropertyName = true)] public string Level { get; set; } protected override void ProcessRecord() { if (Level.Equals(ResponseLogging_All, StringComparison.OrdinalIgnoreCase)) AWSConfigs.LoggingConfig.LogResponses = ResponseLoggingOption.Always; else if (Level.Equals(ResponseLogging_Error, StringComparison.OrdinalIgnoreCase)) AWSConfigs.LoggingConfig.LogResponses = ResponseLoggingOption.OnError; else if (Level.Equals(ResponseLogging_Never, StringComparison.OrdinalIgnoreCase)) AWSConfigs.LoggingConfig.LogResponses = ResponseLoggingOption.Never; else ThrowTerminatingError(new ErrorRecord( new ArgumentException("Level must be one of 'Always', 'OnError', or 'Never'"), "ArgumentException", ErrorCategory.InvalidArgument, Level)); } } [Cmdlet("Enable", "AWSMetricsLogging")] [OutputType("None")] [AWSCmdlet("Enable logging of metrics data for AWS service requests.")] [AWSCmdletOutput("None", "This cmdlet does not produce any output.")] public class EnableMetricsLoggingCmdlet : PSCmdlet { protected override void ProcessRecord() { AWSConfigs.LoggingConfig.LogMetrics = true; } } [Cmdlet("Disable", "AWSMetricsLogging")] [OutputType("None")] [AWSCmdlet("Disable logging of metrics data for AWS service requests.")] [AWSCmdletOutput("None", "This cmdlet does not produce any output.")] public class DisableMetricsLoggingCmdlet : PSCmdlet { protected override void ProcessRecord() { AWSConfigs.LoggingConfig.LogMetrics = false; } } }