Skip to content

Commit

Permalink
新增 AWS 支持
Browse files Browse the repository at this point in the history
  • Loading branch information
Ivony committed Mar 1, 2016
1 parent 33b8742 commit 9958582
Show file tree
Hide file tree
Showing 49 changed files with 82,087 additions and 2 deletions.
44 changes: 44 additions & 0 deletions Ivony.Logs.Aws/CloudWatchLogger.cs
@@ -0,0 +1,44 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Amazon.CloudWatchLogs;

namespace Ivony.Logs.Aws
{
public class CloudWatchLogger : CloudWatchLoggerBase
{



public CloudWatchLogger( AmazonCloudWatchLogsClient client, string groupName, string streamName )
{
_client = client;
GroupName = groupName;
StreamName = streamName;
}


public string GroupName { get; private set; }

public string StreamName { get; private set; }



private AmazonCloudWatchLogsClient _client;

protected override AmazonCloudWatchLogsClient Client { get { return _client; } }


protected override string GetGroupName( LogEntry entry )
{
return GroupName;
}

protected override string GetStreamName( LogEntry entry )
{
return StreamName;
}
}
}
55 changes: 55 additions & 0 deletions Ivony.Logs.Aws/CloudWatchLoggerBase.cs
@@ -0,0 +1,55 @@
using Amazon.CloudWatchLogs;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Amazon.CloudWatchLogs.Model;

namespace Ivony.Logs.Aws
{
public abstract class CloudWatchLoggerBase : Logger
{

public override void LogEntry( LogEntry entry )
{

Client.PutLogEvents( CreateRequest( entry ) );

}


protected abstract AmazonCloudWatchLogsClient Client { get; }


protected virtual PutLogEventsRequest CreateRequest( LogEntry entry )
{
return new PutLogEventsRequest
{
LogEvents = new List<InputLogEvent> { CreateLogEvent( entry ) },
LogGroupName = GetGroupName( entry ),
LogStreamName = GetStreamName( entry ),
};
}

protected abstract string GetStreamName( LogEntry entry );

protected abstract string GetGroupName( LogEntry entry );

protected virtual InputLogEvent CreateLogEvent( LogEntry entry )
{
return new InputLogEvent
{
Message = entry.Message,
Timestamp = entry.LogDate,
};
}







}
}
109 changes: 109 additions & 0 deletions Ivony.Logs.Aws/Ivony.Logs.Aws.csproj
@@ -0,0 +1,109 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProductVersion>9.0.30729</ProductVersion>
<SchemaVersion>2.0</SchemaVersion>
<ProjectGuid>{C34B4882-0280-4EFD-A12A-6E575A93D2B6}</ProjectGuid>
<OutputType>Library</OutputType>
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>Ivony.Logs.Aws</RootNamespace>
<AssemblyName>Ivony.Logs.Aws</AssemblyName>
<TargetFrameworkVersion>v4.5</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
<FileUpgradeFlags>
</FileUpgradeFlags>
<UpgradeBackupLocation>
</UpgradeBackupLocation>
<OldToolsVersion>3.5</OldToolsVersion>
<TargetFrameworkProfile />
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>bin\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<Prefer32Bit>false</Prefer32Bit>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<DebugType>pdbonly</DebugType>
<Optimize>true</Optimize>
<OutputPath>bin\Release\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<Prefer32Bit>false</Prefer32Bit>
</PropertyGroup>
<PropertyGroup>
<StartupObject />
</PropertyGroup>
<Choose>
<When Condition=" '$(TargetFrameworkVersion)' == 'v3.5' Or '$(TargetFrameworkVersion)' == 'v4.0' ">
<ItemGroup>
<Reference Include="AWSSDK.Core, Version=3.0.0.0, Culture=neutral, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion>
<Private>True</Private>
<HintPath>C:\Program Files (x86)\AWS SDK for .NET\bin\Net35\AWSSDK.Core.dll</HintPath>
</Reference>
<Reference Include="AWSSDK.CloudWatch, Version=3.0.0.0, Culture=neutral, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion>
<Private>True</Private>
<HintPath>C:\Program Files (x86)\AWS SDK for .NET\bin\Net35\AWSSDK.CloudWatch.dll</HintPath>
</Reference>
</ItemGroup>
</When>
<Otherwise />
</Choose>
<ItemGroup>
<Reference Include="AWSSDK.CloudWatchLogs, Version=3.1.0.0, Culture=neutral, PublicKeyToken=885c28607f98e604, processorArchitecture=MSIL">
<HintPath>..\packages\AWSSDK.CloudWatchLogs.3.1.2.4\lib\net45\AWSSDK.CloudWatchLogs.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="AWSSDK.Core, Version=3.1.0.0, Culture=neutral, PublicKeyToken=885c28607f98e604, processorArchitecture=MSIL">
<HintPath>..\packages\AWSSDK.Core.3.1.5.0\lib\net45\AWSSDK.Core.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="System" />
<Reference Include="System.configuration" />
<Reference Include="System.Core">
<RequiredTargetFramework>3.5</RequiredTargetFramework>
</Reference>
<Reference Include="System.Xml.Linq">
<RequiredTargetFramework>3.5</RequiredTargetFramework>
</Reference>
<Reference Include="System.Data.DataSetExtensions">
<RequiredTargetFramework>3.5</RequiredTargetFramework>
</Reference>
<Reference Include="System.Data" />
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="CloudWatchLogger.cs" />
<Compile Include="CloudWatchLoggerBase.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
<ItemGroup>
<None Include="packages.config" />
</ItemGroup>
<ItemGroup>
<Analyzer Include="..\packages\AWSSDK.CloudWatchLogs.3.1.2.4\analyzers\dotnet\cs\AWSSDK.CloudWatchLogs.CodeAnalysis.dll" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\Ivony.Logs\Ivony.Logs.csproj">
<Project>{c6dba1a6-13c5-469b-bda9-c19838931205}</Project>
<Name>Ivony.Logs</Name>
</ProjectReference>
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets.
<Target Name="BeforeBuild">
</Target>
<Target Name="AfterBuild">
</Target>
-->
</Project>
36 changes: 36 additions & 0 deletions Ivony.Logs.Aws/Properties/AssemblyInfo.cs
@@ -0,0 +1,36 @@
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;

// General Information about an assembly is controlled through the following
// set of attributes. Change these attribute values to modify the information
// associated with an assembly.
[assembly: AssemblyTitle( "Ivony.Logs.Aws" )]
[assembly: AssemblyDescription( "" )]
[assembly: AssemblyConfiguration( "" )]
[assembly: AssemblyProduct( "Ivony.Logs.Aws" )]
[assembly: AssemblyCompany( "Amazon.com, Inc" )]
[assembly: AssemblyCopyright( "Copyright 2009-2013 Amazon.com, Inc. or its affiliates. All Rights Reserved." )]
[assembly: AssemblyTrademark( "" )]
[assembly: AssemblyCulture( "" )]

// Setting ComVisible to false makes the types in this assembly not visible
// to COM components. If you need to access a type in this assembly from
// COM, set the ComVisible attribute to true on that type.
[assembly: ComVisible( false )]

// The following GUID is for the ID of the typelib if this project is exposed to COM
[assembly: Guid( "3501d4c9-4bbe-4046-ba70-215d4b13dbda" )]

// Version information for an assembly consists of the following four values:
//
// Major Version
// Minor Version
// Build Number
// Revision
//
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion( "1.0.0.0" )]
[assembly: AssemblyFileVersion( "1.0.0.0" )]
5 changes: 5 additions & 0 deletions Ivony.Logs.Aws/packages.config
@@ -0,0 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="AWSSDK.CloudWatchLogs" version="3.1.2.4" targetFramework="net45" />
<package id="AWSSDK.Core" version="3.1.5.0" targetFramework="net45" />
</packages>
6 changes: 6 additions & 0 deletions LogUtility.sln
Expand Up @@ -9,6 +9,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Ivony.Logs.Test", "Ivony.Lo
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "TestConsole", "TestConsole\TestConsole.csproj", "{CD9D622F-898A-4468-AF4B-C49EA31FAC2F}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Ivony.Logs.Aws", "Ivony.Logs.Aws\Ivony.Logs.Aws.csproj", "{C34B4882-0280-4EFD-A12A-6E575A93D2B6}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand All @@ -27,6 +29,10 @@ Global
{CD9D622F-898A-4468-AF4B-C49EA31FAC2F}.Debug|Any CPU.Build.0 = Debug|Any CPU
{CD9D622F-898A-4468-AF4B-C49EA31FAC2F}.Release|Any CPU.ActiveCfg = Release|Any CPU
{CD9D622F-898A-4468-AF4B-C49EA31FAC2F}.Release|Any CPU.Build.0 = Release|Any CPU
{C34B4882-0280-4EFD-A12A-6E575A93D2B6}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{C34B4882-0280-4EFD-A12A-6E575A93D2B6}.Debug|Any CPU.Build.0 = Debug|Any CPU
{C34B4882-0280-4EFD-A12A-6E575A93D2B6}.Release|Any CPU.ActiveCfg = Release|Any CPU
{C34B4882-0280-4EFD-A12A-6E575A93D2B6}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down
8 changes: 6 additions & 2 deletions TestConsole/Program.cs
@@ -1,4 +1,7 @@
using Ivony.Logs;
using Amazon;
using Amazon.CloudWatchLogs;
using Ivony.Logs;
using Ivony.Logs.Aws;
using System;
using System.Collections.Generic;
using System.Diagnostics;
Expand All @@ -14,7 +17,8 @@ class Program
static void Main( string[] args )
{

var logger = /*new ConsoleLogger() +*/ new TextFileLogger( @"C:\Temp\Logs\1.log" ) + new TextFileLogger( new DirectoryInfo( @"C:\Temp\Logs\Test" ) );
var logger = new ConsoleLogger() + new TextFileLogger( @"C:\Temp\Logs\1.log" ) + new TextFileLogger( new DirectoryInfo( @"C:\Temp\Logs\Test" ) )
+ new CloudWatchLogger( new AmazonCloudWatchLogsClient( "AKIAIT5TCZWUB6ROTUQQ", "0HjXbLn4d6wPr0Y4Nu0xNxc2M9A3dwjLY0y8gJji", RegionEndpoint.APSoutheast1 ), "Test", "Test" );



Expand Down
16 changes: 16 additions & 0 deletions TestConsole/TestConsole.csproj
Expand Up @@ -32,6 +32,14 @@
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<ItemGroup>
<Reference Include="AWSSDK.CloudWatchLogs, Version=3.1.0.0, Culture=neutral, PublicKeyToken=885c28607f98e604, processorArchitecture=MSIL">
<HintPath>..\packages\AWSSDK.CloudWatchLogs.3.1.2.4\lib\net45\AWSSDK.CloudWatchLogs.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="AWSSDK.Core, Version=3.1.0.0, Culture=neutral, PublicKeyToken=885c28607f98e604, processorArchitecture=MSIL">
<HintPath>..\packages\AWSSDK.Core.3.1.5.0\lib\net45\AWSSDK.Core.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="System" />
<Reference Include="System.Core" />
<Reference Include="System.Xml.Linq" />
Expand All @@ -46,13 +54,21 @@
</ItemGroup>
<ItemGroup>
<None Include="App.config" />
<None Include="packages.config" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\Ivony.Logs.Aws\Ivony.Logs.Aws.csproj">
<Project>{c34b4882-0280-4efd-a12a-6e575a93d2b6}</Project>
<Name>Ivony.Logs.Aws</Name>
</ProjectReference>
<ProjectReference Include="..\Ivony.Logs\Ivony.Logs.csproj">
<Project>{c6dba1a6-13c5-469b-bda9-c19838931205}</Project>
<Name>Ivony.Logs</Name>
</ProjectReference>
</ItemGroup>
<ItemGroup>
<Analyzer Include="..\packages\AWSSDK.CloudWatchLogs.3.1.2.4\analyzers\dotnet\cs\AWSSDK.CloudWatchLogs.CodeAnalysis.dll" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets.
Expand Down
5 changes: 5 additions & 0 deletions TestConsole/packages.config
@@ -0,0 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="AWSSDK.CloudWatchLogs" version="3.1.2.4" targetFramework="net45" />
<package id="AWSSDK.Core" version="3.1.5.0" targetFramework="net45" />
</packages>
Binary file not shown.
Binary file not shown.

0 comments on commit 9958582

Please sign in to comment.