-
Notifications
You must be signed in to change notification settings - Fork 147
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #123 from MV10/ms_config_v2
- Loading branch information
Showing
14 changed files
with
696 additions
and
106 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
82 changes: 82 additions & 0 deletions
82
src/Serilog.Sinks.MSSqlServer/Configuration/ConvertSqlDataTypecs.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
using System; | ||
using System.Data; | ||
|
||
namespace Serilog.Configuration | ||
{ | ||
/// <summary> | ||
/// Converts the SQL data types of custom columns specified | ||
/// in app configuration to the equivalent .NET types | ||
/// </summary> | ||
public static class ConvertSqlDataType | ||
{ | ||
/// <summary> | ||
/// Converts the SQL data types of custom columns specified | ||
/// in app configuration to the equivalent .NET types | ||
/// </summary> | ||
/// <param name="sqlDataType">The SQL data type of custom columns from app configuration</param> | ||
/// <param name="length">A length applied to types that accept a length</param> | ||
/// <returns></returns> | ||
public static DataColumn GetEquivalentType(string sqlDataType, int length = 0) | ||
{ | ||
DataColumn c = new DataColumn(); | ||
switch(sqlDataType) | ||
{ | ||
case "bigint": | ||
c.DataType = typeof(long); | ||
break; | ||
case "varbinary": | ||
case "binary": | ||
if(length == 0) throw new ArgumentException($"SQL {sqlDataType} column requires a non-zero length argument."); | ||
c.DataType = Type.GetType("System.Byte[]"); | ||
c.ExtendedProperties["DataLength"] = length; | ||
break; | ||
case "bit": | ||
c.DataType = typeof(bool); | ||
break; | ||
case "char": | ||
case "nchar": | ||
case "ntext": | ||
case "nvarchar": | ||
case "text": | ||
case "varchar": | ||
if(length == 0) throw new ArgumentException($"SQL {sqlDataType} column requires a non-zero length argument."); | ||
c.DataType = Type.GetType("System.String"); | ||
c.MaxLength = length; | ||
break; | ||
case "date": | ||
case "datetime": | ||
case "datetime2": | ||
case "smalldatetime": | ||
c.DataType = typeof(DateTime); | ||
break; | ||
case "decimal": | ||
case "money": | ||
case "numeric": | ||
case "smallmoney": | ||
c.DataType = typeof(Decimal); | ||
break; | ||
case "float": | ||
c.DataType = typeof(double); | ||
break; | ||
case "int": | ||
c.DataType = typeof(int); | ||
break; | ||
case "real": | ||
c.DataType = typeof(float); | ||
break; | ||
case "smallint": | ||
c.DataType = typeof(short); | ||
break; | ||
case "time": | ||
c.DataType = typeof(TimeSpan); | ||
break; | ||
case "uniqueidentifier": | ||
c.DataType = typeof(Guid); | ||
break; | ||
default: | ||
throw new ArgumentException($"SQL {sqlDataType} is not a recognized or supported column data-type."); | ||
} | ||
return c; | ||
} | ||
} | ||
} |
63 changes: 63 additions & 0 deletions
63
src/Serilog.Sinks.MSSqlServer/Configuration/Microsoft.Extensions.Configuration/Column.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
// Copyright 2015 Serilog Contributors | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License 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. | ||
|
||
using System.Text.RegularExpressions; | ||
|
||
namespace Serilog.Configuration | ||
{ | ||
/// <summary> | ||
/// Details of an individual column described in the app config. | ||
/// </summary> | ||
public class Column | ||
{ | ||
private string _dataType = "varchar"; | ||
private const string ValidSqlDataTypes = "(bigint)|(bit)|(binary)|(varbinary)|(char)|(date)|(datetime)|(datetime2)|(decimal)|(float)|(int)|(money)|(nchar)|(ntext)|(numeric)|(nvarchar)|(real)|(smalldatetime)|(smallint)|(smallmoney)|(text)|(time)|(uniqueidentifier)|(varchar)"; | ||
|
||
/// <summary> | ||
/// The name of a custom SQL Server column. | ||
/// </summary> | ||
public string ColumnName { get; set; } | ||
|
||
/// <summary> | ||
/// The data type of a custom SQL Server column. | ||
/// </summary> | ||
public string DataType | ||
{ | ||
get => _dataType; | ||
set | ||
{ | ||
if(Regex.Match(value, ValidSqlDataTypes).Success) | ||
{ | ||
_dataType = value; | ||
} | ||
else | ||
{ | ||
// If validation fails, ignore the value, which matches the behavior | ||
// of the .NET Framework [RegexStringValidator] attribute used by the | ||
// original version of the SQL sink project. | ||
} | ||
} | ||
} | ||
|
||
/// <summary> | ||
/// The size of certain SQL Server column types such as varchar; | ||
/// </summary> | ||
public int DataLength { get; set; } = 0; | ||
|
||
/// <summary> | ||
/// Controls whether the column is nullable | ||
/// </summary> | ||
public bool AllowNull { get; set; } = true; | ||
} | ||
} |
Oops, something went wrong.