1
0
mirror of https://github.com/BDDSM/YY.EventLogReaderAssistant.git synced 2024-11-21 10:05:51 +02:00

Инициализация проекта библиотеки YY.EventLogAssistant

This commit is contained in:
YPermitin 2020-04-09 23:54:05 +05:00
parent a681ecad36
commit 64ce7c1c92
27 changed files with 2311 additions and 0 deletions

View File

@ -0,0 +1,25 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 16
VisualStudioVersion = 16.0.30002.166
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "YY.EventLogAssistant", "YY.EventLogAssistant\YY.EventLogAssistant.csproj", "{5B2E13F6-9F93-46AA-AB61-7B3A4331840D}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{5B2E13F6-9F93-46AA-AB61-7B3A4331840D}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{5B2E13F6-9F93-46AA-AB61-7B3A4331840D}.Debug|Any CPU.Build.0 = Debug|Any CPU
{5B2E13F6-9F93-46AA-AB61-7B3A4331840D}.Release|Any CPU.ActiveCfg = Release|Any CPU
{5B2E13F6-9F93-46AA-AB61-7B3A4331840D}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {9F628392-F4A2-4E4C-B478-BDB29B256BA4}
EndGlobalSection
EndGlobal

View File

@ -0,0 +1,390 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data.SQLite;
using YY.LogReader.Models;
using YY.LogReader.Services;
using YY.LogReader.Models.EventLog;
namespace YY.LogReader.EventLog
{
internal sealed class EventLogLGDReader : EventLogReader
{
private string _connectionString;
private string ConnectionString
{
get
{
if(_connectionString == null)
_connectionString = SQLiteExtensions.GetConnectionString(_logFilePath);
return _connectionString;
}
}
private SQLiteConnection _connection;
private List<EventLogRowData> _readBuffer;
private long _lastRowId;
private const int _readBufferSize = 10000;
private long _lastRowNumberFromBuffer;
private long _eventCount = -1;
internal EventLogLGDReader() : base() { }
internal EventLogLGDReader(string logFilePath) : base(logFilePath)
{
_readBuffer = new List<EventLogRowData>();
_lastRowId = 0;
_lastRowNumberFromBuffer = 0;
}
public override bool Read(out EventLogRowData rowData)
{
try
{
BeforeReadFileEventArgs beforeReadFileArgs = new BeforeReadFileEventArgs(_logFilePath);
if (_eventCount < 0)
RaiseBeforeReadFile(beforeReadFileArgs);
if (beforeReadFileArgs.Cancel)
{
rowData = null;
return false;
}
#region bufferedRead
if (_lastRowNumberFromBuffer == 0
|| _lastRowNumberFromBuffer >= _readBufferSize)
{
_readBuffer.Clear();
_lastRowNumberFromBuffer = 0;
using (_connection = new SQLiteConnection(ConnectionString))
{
_connection.Open();
string queryText = String.Format(
"Select\n" +
" el.RowId,\n" +
" el.Date AS Date,\n" +
" el.ConnectId,\n" +
" el.Session,\n" +
" el.TransactionStatus,\n" +
" el.TransactionDate,\n" +
" el.TransactionId,\n" +
" el.UserCode AS UserCode,\n" +
" el.ComputerCode AS ComputerCode,\n" +
" el.appCode AS ApplicationCode,\n" +
" el.eventCode AS EventCode,\n" +
" el.primaryPortCode AS PrimaryPortCode,\n" +
" el.secondaryPortCode AS SecondaryPortCode,\n" +
" el.workServerCode AS WorkServerCode,\n" +
" el.Severity AS SeverityCode,\n" +
" el.Comment AS Comment,\n" +
" el.Data AS Data,\n" +
" el.DataPresentation AS DataPresentation,\n" +
" elm.metadataCode AS MetadataCode\n" +
"From\n" +
" EventLog el\n" +
" left join EventLogMetadata elm on el.RowId = elm.eventLogID\n" +
" left join MetadataCodes mc on elm.metadataCode = mc.code\n" +
"Where RowID > {0}\n" +
"Order By rowID\n" +
"Limit {1}\n", _lastRowId, _readBufferSize);
using (SQLiteCommand cmd = new SQLiteCommand(queryText, _connection))
{
using (SQLiteDataReader reader = cmd.ExecuteReader())
{
while (reader.Read())
{
try
{
EventLogRowData bufferRowData = new EventLogRowData();
bufferRowData.RowID = reader.GetInt64OrDefault(0);
bufferRowData.Period = reader.GetInt64OrDefault(1).ToDateTimeFormat();
bufferRowData.ConnectId = reader.GetInt64OrDefault(2);
bufferRowData.Session = reader.GetInt64OrDefault(3);
bufferRowData.TransactionStatus = (TransactionStatus)reader.GetInt64OrDefault(4);
bufferRowData.TransactionDate = reader.GetInt64OrDefault(5).ToNullableDateTimeELFormat();
bufferRowData.TransactionId = reader.GetInt64OrDefault(6);
bufferRowData.Severity = (Severity)reader.GetInt64OrDefault(15);
bufferRowData.Comment = reader.GetStringOrDefault(16);
bufferRowData.Data = reader.GetStringOrDefault(17).FromWIN1251ToUTF8();
bufferRowData.DataPresentation = reader.GetStringOrDefault(18);
bufferRowData.User = _users.Where(i => i.Code == reader.GetInt64OrDefault(7)).FirstOrDefault();
bufferRowData.Computer = _computers.Where(i => i.Code == reader.GetInt64OrDefault(8)).FirstOrDefault();
bufferRowData.Application = _applications.Where(i => i.Code == reader.GetInt64OrDefault(9)).FirstOrDefault();
bufferRowData.Event = _events.Where(i => i.Code == reader.GetInt64OrDefault(10)).FirstOrDefault();
bufferRowData.PrimaryPort = _primaryPorts.Where(i => i.Code == reader.GetInt64OrDefault(11)).FirstOrDefault();
bufferRowData.SecondaryPort = _secondaryPorts.Where(i => i.Code == reader.GetInt64OrDefault(12)).FirstOrDefault();
bufferRowData.WorkServer = _workServers.Where(i => i.Code == reader.GetInt64OrDefault(13)).FirstOrDefault();
bufferRowData.Metadata = _metadata.Where(i => i.Code == reader.GetInt64OrDefault(18)).FirstOrDefault();
_readBuffer.Add(bufferRowData);
}
catch (Exception ex)
{
RaiseOnError(new OnErrorEventArgs(ex, reader.GetRowAsString(), false));
rowData = null;
}
}
}
}
}
}
#endregion
if (_lastRowNumberFromBuffer >= _readBuffer.Count)
{
RaiseAfterReadFile(new AfterReadFileEventArgs(_logFilePath));
rowData = null;
return false;
}
RaiseBeforeRead(new BeforeReadEventArgs(null, _eventCount));
rowData = _readBuffer
.Where(bufRow => bufRow.RowID > _lastRowId)
.First();
_lastRowNumberFromBuffer = _lastRowNumberFromBuffer + 1;
_lastRowId = rowData.RowID;
RaiseAfterRead(new AfterReadEventArgs(rowData, _eventCount));
return true;
}
catch(Exception ex)
{
RaiseOnError(new OnErrorEventArgs(ex, null, true));
rowData = null;
return false;
}
}
public override bool GoToEvent(long eventNumber)
{
Reset();
long eventCount = Count();
if (eventCount >= eventNumber)
{
using (_connection = new SQLiteConnection(ConnectionString))
{
_connection.Open();
string queryText = String.Format(
"Select\n" +
" el.RowId,\n" +
"From\n" +
" EventLog el\n" +
"Where RowID > {0}\n" +
"Order By rowID\n" +
"Limit 1 OFFSET {1}\n", _lastRowId, eventNumber);
using (SQLiteCommand cmd = new SQLiteCommand(queryText, _connection))
{
using (SQLiteDataReader reader = cmd.ExecuteReader())
{
if (reader.Read())
{
_lastRowId = reader.GetInt64OrDefault(0);
return true;
}
}
}
}
}
return false;
}
public override EventLogPosition GetCurrentPosition()
{
return new EventLogPosition(
_currentFileEventNumber,
_logFilePath,
_logFilePath,
null);
}
public override void SetCurrentPosition(EventLogPosition newPosition)
{
if (newPosition.CurrentFileReferences != _logFilePath)
throw new Exception("Invalid data file with references");
if (newPosition.CurrentFileData != _logFilePath)
throw new Exception("Invalid data file with references");
GoToEvent(newPosition.EventNumber);
}
public override long Count()
{
if (_eventCount < 0)
{
using (_connection = new SQLiteConnection(ConnectionString))
{
_connection.Open();
string queryText = String.Format(
"Select\n" +
" COUNT(el.RowId) CNT\n" +
"From\n" +
" EventLog el\n");
SQLiteCommand cmd = new SQLiteCommand(_connection);
cmd.CommandType = System.Data.CommandType.Text;
cmd.CommandText = queryText;
SQLiteDataReader reader = cmd.ExecuteReader();
if (reader.Read())
{
_eventCount = reader.GetInt64OrDefault(0);
}
}
}
return _eventCount;
}
public override void Reset()
{
System.Data.SQLite.SQLiteConnection.ClearAllPools();
if (_connection != null)
{
_connection.Dispose();
}
_lastRowId = 0;
_lastRowNumberFromBuffer = 0;
_readBuffer.Clear();
}
public override void Dispose()
{
base.Dispose();
System.Data.SQLite.SQLiteConnection.ClearAllPools();
if(_connection != null)
{
_connection.Dispose();
}
_readBuffer.Clear();
}
protected override void ReadEventLogReferences()
{
using (_connection = new SQLiteConnection(ConnectionString))
{
_connection.Open();
using (SQLiteCommand cmdUserCodes = new SQLiteCommand("Select Code, Name, UUID From UserCodes", _connection))
{
using (SQLiteDataReader readerUserCodes = cmdUserCodes.ExecuteReader())
while (readerUserCodes.Read())
{
_users.Add(new Users()
{
Code = readerUserCodes.GetInt64(0),
Name = readerUserCodes.GetString(1),
Uuid = readerUserCodes.GetString(2).ToGuid()
});
}
}
using (SQLiteCommand cmdComputerCodes = new SQLiteCommand("Select Code, Name From ComputerCodes", _connection))
{
using (SQLiteDataReader readerComputerCodes = cmdComputerCodes.ExecuteReader())
while (readerComputerCodes.Read())
{
_computers.Add(new Computers()
{
Code = readerComputerCodes.GetInt64(0),
Name = readerComputerCodes.GetString(1)
});
}
}
using (SQLiteCommand cmdEventCodes = new SQLiteCommand("Select Code, Name From EventCodes", _connection))
{
using (SQLiteDataReader readerEventCodes = cmdEventCodes.ExecuteReader())
while (readerEventCodes.Read())
{
_events.Add(new Events()
{
Code = readerEventCodes.GetInt64(0),
Name = readerEventCodes.GetString(1)
});
}
}
using (SQLiteCommand cmdMetadataCodes = new SQLiteCommand("Select Code, Name, UUID From MetadataCodes", _connection))
{
using (SQLiteDataReader readerMetadataCodes = cmdMetadataCodes.ExecuteReader())
while (readerMetadataCodes.Read())
{
_metadata.Add(new Metadata()
{
Code = readerMetadataCodes.GetInt64(0),
Name = readerMetadataCodes.GetString(1),
Uuid = readerMetadataCodes.GetString(2).ToGuid()
});
}
}
using (SQLiteCommand cmdAppCodes = new SQLiteCommand("Select Code, Name From AppCodes", _connection))
{
using (SQLiteDataReader readerAppCodes = cmdAppCodes.ExecuteReader())
while (readerAppCodes.Read())
{
_applications.Add(new Applications()
{
Code = readerAppCodes.GetInt64(0),
Name = readerAppCodes.GetString(1)
});
}
}
using (SQLiteCommand cmdWorkServerCodes = new SQLiteCommand("Select Code, Name From WorkServerCodes", _connection))
{
using (SQLiteDataReader readerWorkServerCodes = cmdWorkServerCodes.ExecuteReader())
while (readerWorkServerCodes.Read())
{
_workServers.Add(new WorkServers()
{
Code = readerWorkServerCodes.GetInt64(0),
Name = readerWorkServerCodes.GetString(1)
});
}
}
using (SQLiteCommand cmdPrimaryPortCodes = new SQLiteCommand("Select Code, Name From PrimaryPortCodes", _connection))
{
using (SQLiteDataReader readerPrimaryPortCodes = cmdPrimaryPortCodes.ExecuteReader())
while (readerPrimaryPortCodes.Read())
{
_primaryPorts.Add(new PrimaryPorts()
{
Code = readerPrimaryPortCodes.GetInt64(0),
Name = readerPrimaryPortCodes.GetString(1)
});
}
}
using (SQLiteCommand cmdSecondaryPortCodes = new SQLiteCommand("Select Code, Name From SecondaryPortCodes", _connection))
{
using (SQLiteDataReader readerSecondaryPortCodes = cmdSecondaryPortCodes.ExecuteReader())
while (readerSecondaryPortCodes.Read())
{
_secondaryPorts.Add(new SecondaryPorts()
{
Code = readerSecondaryPortCodes.GetInt64(0),
Name = readerSecondaryPortCodes.GetString(1)
});
}
}
}
}
}
}

View File

@ -0,0 +1,631 @@
using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using YY.LogReader.Models;
using YY.LogReader.Models.EventLog;
using YY.LogReader.Services;
namespace YY.LogReader.EventLog
{
internal sealed class EventLogLGFReader : EventLogReader
{
private const long _defaultBeginLineForLGF = 3;
private int _indexCurrentFile;
private string[] _logFilesWithData;
public string CurrentFile
{
get
{
if (_logFilesWithData.Length <= _indexCurrentFile)
return null;
else
return _logFilesWithData[_indexCurrentFile];
}
}
private long _eventCount = -1;
StreamReader _stream;
StringBuilder _eventSource;
LogParserLGF _logParser;
private LogParserLGF LogParser
{
get
{
if (_logParser == null)
_logParser = new LogParserLGF(this);
return _logParser;
}
}
internal EventLogLGFReader() : base() { }
internal EventLogLGFReader(string logFilePath) : base(logFilePath)
{
_indexCurrentFile = 0;
_logFilesWithData = Directory
.GetFiles(_logFileDirectoryPath, "*.lgp")
.OrderBy(i => i)
.ToArray();
_eventSource = new StringBuilder();
}
public override bool Read(out EventLogRowData rowData)
{
try
{
if (_stream == null)
{
if (_logFilesWithData.Length <= _indexCurrentFile)
{
rowData = null;
return false;
}
InitializeStream(_defaultBeginLineForLGF, _indexCurrentFile);
_currentFileEventNumber = 0;
}
_eventSource.Clear();
BeforeReadFileEventArgs beforeReadFileArgs = new BeforeReadFileEventArgs(CurrentFile);
if (_currentFileEventNumber == 0)
RaiseBeforeReadFile(beforeReadFileArgs);
if (beforeReadFileArgs.Cancel)
{
NextFile();
return Read(out rowData);
}
string sourceData;
bool newLine = true;
int countBracket = 0;
bool textBlockOpen = false;
while (true)
{
sourceData = _stream.ReadLine();
if (sourceData == null)
{
NextFile();
return Read(out rowData);
}
if (newLine)
{
_eventSource.Append(sourceData);
}
else
{
_eventSource.AppendLine();
_eventSource.Append(sourceData);
}
if (_logParser.ItsEndOfEvent(sourceData, ref countBracket, ref textBlockOpen))
{
newLine = true;
_currentFileEventNumber += 1;
string prepearedSourceData = _eventSource.ToString();
RaiseBeforeRead(new BeforeReadEventArgs(prepearedSourceData, _currentFileEventNumber));
try
{
EventLogRowData eventData = LogParser.Parse(prepearedSourceData);
rowData = eventData;
}
catch (Exception ex)
{
RaiseOnError(new OnErrorEventArgs(ex, prepearedSourceData, false));
rowData = null;
}
RaiseAfterRead(new AfterReadEventArgs(rowData, _currentFileEventNumber));
return true;
}
else
{
newLine = false;
}
}
}
catch (Exception ex)
{
RaiseOnError(new OnErrorEventArgs(ex, null, true));
rowData = null;
return false;
}
}
public override bool GoToEvent(long eventNumber)
{
Reset();
int fileIndex = -1;
long currentLineNumber = -1;
long currentEventNumber = 0;
bool moved = false;
foreach (string logFile in _logFilesWithData)
{
fileIndex += 1;
currentLineNumber = -1;
IEnumerable<string> allLines = File.ReadLines(logFile);
foreach (string line in allLines)
{
currentLineNumber += 1;
if (Regex.IsMatch(line, @"^{\d{4}\d{2}\d{2}\d+"))
{
currentEventNumber += 1;
}
if (currentEventNumber == eventNumber)
{
moved = true;
break;
}
}
if (currentEventNumber == eventNumber)
{
moved = true;
break;
}
}
if (moved && fileIndex >= 0 && currentLineNumber >= 0)
{
InitializeStream(currentLineNumber, fileIndex);
_eventCount = eventNumber - 1;
return true;
}
else
{
return false;
}
}
public override EventLogPosition GetCurrentPosition()
{
return new EventLogPosition(
_currentFileEventNumber,
_logFilePath,
CurrentFile,
GetCurrentFileStreamPosition());
}
public override void SetCurrentPosition(EventLogPosition newPosition)
{
Reset();
if(newPosition.CurrentFileReferences != _logFilePath)
throw new Exception("Invalid data file with references");
int indexOfFileData = Array.IndexOf(_logFilesWithData, newPosition.CurrentFileData);
if (indexOfFileData < 0)
throw new Exception("Invalid data file");
_indexCurrentFile = indexOfFileData;
_currentFileEventNumber = newPosition.EventNumber;
InitializeStream(_defaultBeginLineForLGF, _indexCurrentFile);
if (newPosition.StreamPosition != null)
SetCurrentFileStreamPosition((long)newPosition.StreamPosition);
}
public override long Count()
{
if(_eventCount < 0)
_eventCount = LogParser.GetEventCount();
return _eventCount;
}
public override void Reset()
{
if (_stream != null)
{
_stream.Dispose();
_stream = null;
}
_indexCurrentFile = 0;
}
public override void NextFile()
{
RaiseAfterReadFile(new AfterReadFileEventArgs(CurrentFile));
if (_stream != null)
{
_stream.Dispose();
_stream = null;
}
_indexCurrentFile += 1;
}
public override void Dispose()
{
base.Dispose();
if (_stream != null)
{
_stream.Dispose();
_stream = null;
}
}
public long GetCurrentFileStreamPosition()
{
if (_stream != null)
return _stream.GetPosition();
else
return 0;
}
public void SetCurrentFileStreamPosition(long position)
{
if (_stream != null)
_stream.SetPosition(position);
}
protected override void ReadEventLogReferences()
{
string Text = string.Empty;
using (FileStream FS = new FileStream(_logFilePath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
using (StreamReader SR = new StreamReader(FS))
Text = SR.ReadToEnd();
int beginBlockIndex = Text.IndexOf("{");
if (beginBlockIndex < 0)
return;
Text = Text.Substring(beginBlockIndex);
string[] ObjectTexts = LogParser.ParseEventLogString("{" + Text + "}");
string LastProcessedObjectForDebug;
foreach (string TextObject in ObjectTexts)
{
LastProcessedObjectForDebug = TextObject;
string[] a = LogParser.ParseEventLogString(TextObject);
if ((a != null))
{
switch (a[0])
{
case "1":
_users.Add(new Users()
{
Code = long.Parse(a[3]),
Uuid = a[1].ToGuid(),
Name = a[2]
});
break;
case "2":
_computers.Add(new Computers()
{
Code = int.Parse(a[2]),
Name = a[1]
});
break;
case "3":
_applications.Add(new Applications()
{
Code = int.Parse(a[2]),
Name = a[1]
});
break;
case "4":
_events.Add(new Events()
{
Code = int.Parse(a[2]),
Name = a[1]
});
break;
case "5":
_metadata.Add(new Metadata()
{
Code = int.Parse(a[3]),
Uuid = a[1].ToGuid(),
Name = a[2]
});
break;
case "6":
_workServers.Add(new WorkServers()
{
Code = int.Parse(a[2]),
Name = a[1]
});
break;
case "7":
_primaryPorts.Add(new PrimaryPorts()
{
Code = int.Parse(a[2]),
Name = a[1]
});
break;
case "8":
_secondaryPorts.Add(new SecondaryPorts()
{
Code = int.Parse(a[2]),
Name = a[1]
});
break;
//Case "9" - эти значения отсутствуют в файле
//Case "10"
case "11":
break;
case "12":
break;
case "13":
break;
// Последние три значения содержат статус транзакции и важность события
default:
break;
}
}
}
}
private void InitializeStream(long linesToSkip, int fileIndex = 0)
{
FileStream fs = new FileStream(_logFilesWithData[fileIndex], FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
_stream = new StreamReader(fs);
_stream.SkipLine(linesToSkip);
}
private sealed class LogParserLGF
{
private EventLogLGFReader _reader;
public LogParserLGF(EventLogLGFReader reader)
{
_reader = reader;
}
public EventLogRowData Parse(string eventSource)
{
var parseResult = ParseEventLogString(eventSource);
DateTime eventDate = DateTime.ParseExact(parseResult[0], "yyyyMMddHHmmss", CultureInfo.InvariantCulture);
EventLogRowData eventData = new EventLogRowData();
eventData.RowID = _reader.CurrentFileEventNumber;
eventData.Period = eventDate;
string TransStr = parseResult[2].ToString().Replace("}", "").Replace("{", "");
long TransDate = TransStr.Substring(0, TransStr.IndexOf(",")).From16To10();
try
{
if (!(TransDate == 0))
eventData.TransactionDate = new System.DateTime().AddSeconds((double)TransDate / 10000);
}
catch
{
eventData.TransactionDate = null;
}
eventData.TransactionId = TransStr.Substring(TransStr.IndexOf(",") + 1).From16To10();
if (parseResult[1] == "R")
eventData.TransactionStatus = TransactionStatus.Unfinished;
else if (parseResult[1] == "N")
eventData.TransactionStatus = TransactionStatus.NotApplicable;
else if (parseResult[1] == "U")
eventData.TransactionStatus = TransactionStatus.Committed;
else if (parseResult[1] == "C")
eventData.TransactionStatus = TransactionStatus.RolledBack;
else
eventData.TransactionStatus = TransactionStatus.Unknown;
eventData.Comment = parseResult[9].RemoveQuotes();
eventData.Session = Convert.ToInt32(parseResult[16]);
eventData.ConnectId = Convert.ToInt32(parseResult[6]);
eventData.DataPresentation = parseResult[12].RemoveQuotes();
eventData.Data = parseResult[11];
MatchCollection matches = Regex.Matches(eventData.Data, @"[\d]+:[\dA-Za-zА-Яа-я]{32}}");
if (matches.Count > 0)
{
string[] dataPartsUUID = eventData.Data.Split(':');
if (dataPartsUUID.Length == 2)
{
string dataUUID = dataPartsUUID[1].Replace("}", string.Empty);
eventData.DataUUID = dataUUID;
}
}
long userID = Convert.ToInt64(parseResult[3]);
eventData.User = _reader.Users.Where(i => i.Code == userID).FirstOrDefault();
long computerID = Convert.ToInt64(parseResult[4]);
eventData.Computer = _reader.Computers.Where(i => i.Code == computerID).FirstOrDefault();
long appID = Convert.ToInt64(parseResult[5]);
eventData.Application = _reader.Applications.Where(i => i.Code == appID).FirstOrDefault();
long eventID = Convert.ToInt64(parseResult[7]);
eventData.Event = _reader.Events.Where(i => i.Code == eventID).FirstOrDefault();
long metadataID = Convert.ToInt64(parseResult[10]);
eventData.Metadata = _reader.Metadata.Where(i => i.Code == metadataID).FirstOrDefault();
long workServerID = Convert.ToInt64(parseResult[13]);
eventData.WorkServer = _reader.WorkServers.Where(i => i.Code == workServerID).FirstOrDefault();
long pimaryPortID = Convert.ToInt64(parseResult[14]);
eventData.PrimaryPort = _reader.PrimaryPorts.Where(i => i.Code == pimaryPortID).FirstOrDefault();
long secondaryPortID = Convert.ToInt64(parseResult[15]);
eventData.SecondaryPort = _reader.SecondaryPorts.Where(i => i.Code == secondaryPortID).FirstOrDefault();
if (eventData.Data == "{\"U\"}") // 'empty reference
eventData.Data = string.Empty;
else if (eventData.Data.StartsWith("{"))
{
//'internal representation for different objects.
var ParsedObject = ParseEventLogString(eventData.Data);
if (ParsedObject.Length == 2)
{
if (ParsedObject[0] == "\"S\"" || ParsedObject[0] == "\"R\"")
{
//'this is string or reference
eventData.Data = ParsedObject[1].RemoveQuotes(); // 'string value
}
}
}
switch (parseResult[8].Trim())
{
case "I":
eventData.Severity = Severity.Information;
break;
case "W":
eventData.Severity = Severity.Warning;
break;
case "E":
eventData.Severity = Severity.Error;
break;
case "N":
eventData.Severity = Severity.Note;
break;
default:
eventData.Severity = Severity.Unknown;
break;
}
return eventData;
}
internal bool ItsEndOfEvent(string Str, ref int Count, ref bool TextBlockOpen)
{
string TempStr = Str;
for (int i = 0; i <= TempStr.Length - 1; i++)
{
string Simb = TempStr.Substring(i, 1);
if (Simb == "\"")
{
TextBlockOpen = !TextBlockOpen;
}
else if (Simb == "}" & !TextBlockOpen)
{
Count = Count - 1;
}
else if (Simb == "{" & !TextBlockOpen)
{
Count = Count + 1;
}
}
return (Count == 0);
}
internal string[] ParseEventLogString(string Text)
{
string[] ArrayLines = null;
string Text2 = Text.Substring(1, (Text.EndsWith(",") ? Text.Length - 3 : Text.Length - 2)) + ",";
string Str = "";
int Delim = Text2.IndexOf(",");
int i = 0;
int partNumber = 0;
bool isSpecialString = false;
while (Delim > 0)
{
Str = Str + Text2.Substring(0, Delim).Trim();
partNumber += 1;
Text2 = Text2.Substring(Delim + 1);
if (partNumber == 1 && !String.IsNullOrEmpty(Str) && Str[0] == '\"')
isSpecialString = true;
int count1;
int count2;
if (isSpecialString)
{
count1 = 0;
count2 = 0;
}
else
{
count1 = CountSubstringInString(Str, "{");
count2 = CountSubstringInString(Str, "}");
}
int count3 = CountSubstringInString(Str, "\"") % 2;//Math.IEEERemainder(CountSubstringInString(Str, "\""), 2);
if (count1 == count2 & count3 == 0)
{
Array.Resize(ref ArrayLines, i + 1);
if (Str.StartsWith("\"") && Str.EndsWith("\""))
{
Str = Str.Substring(1, Str.Length - 2);
}
if (isSpecialString)
{
char[] denied = new[] { '\n', '\t', '\r' };
StringBuilder newString = new StringBuilder();
foreach (var ch in Str)
if (!denied.Contains(ch))
newString.Append(ch);
Str = newString.ToString();
}
ArrayLines[i] = Str;
i = i + 1;
Str = "";
partNumber = 0;
isSpecialString = false;
}
else
{
Str = Str + ",";
}
if (isSpecialString) // Особая обработка для поля "DataPresentation"
{
Delim = Text2.IndexOf("\",") + 1;
}
else
{
Delim = Text2.IndexOf(",");
}
}
return ArrayLines;
}
private int CountSubstringInString(string Str, string SubStr)
{
return (Str.Length - Str.Replace(SubStr, "").Length) / SubStr.Length;
}
public long GetEventCount()
{
long eventCount = 0;
foreach (var logFile in _reader._logFilesWithData)
{
eventCount += File.ReadLines(logFile)
.Where(lineInt => Regex.IsMatch(lineInt, @"^{\d{4}\d{2}\d{2}\d+,"))
.LongCount();
}
return eventCount;
}
}
}
}

View File

@ -0,0 +1,27 @@
using System;
using System.Collections.Generic;
using System.Text;
namespace YY.LogReader.EventLog
{
public sealed class EventLogPosition
{
private readonly long _eventNumber;
private readonly string _currentFileReferences;
private readonly string _currentFileData;
private readonly long? _streamPosition;
public EventLogPosition(long EventNumber, string CurrentFileReferences, string CurrentFileData, long? StreamPosition)
{
_eventNumber = EventNumber;
_currentFileReferences = CurrentFileReferences;
_currentFileData = CurrentFileData;
_streamPosition = StreamPosition;
}
public long EventNumber { get { return _eventNumber; } }
public string CurrentFileReferences { get { return _currentFileReferences; } }
public string CurrentFileData { get { return _currentFileData; } }
public long? StreamPosition { get { return _streamPosition; } }
}
}

View File

@ -0,0 +1,234 @@
using YY.LogReader.Models.EventLog;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
namespace YY.LogReader.EventLog
{
public abstract class EventLogReader : IEventLogReader, IDisposable
{
public static EventLogReader CreateReader(string pathLogFile)
{
FileAttributes attr = File.GetAttributes(pathLogFile);
FileInfo logFileInfo = null;
string currentLogFilesPath;
string logFileWithReferences;
if ((attr & FileAttributes.Directory) == FileAttributes.Directory)
{
currentLogFilesPath = pathLogFile;
logFileWithReferences = string.Format("{0}{1}{2}", currentLogFilesPath, Path.DirectorySeparatorChar, @"1Cv8.lgf");
}
else
{
logFileInfo = new FileInfo(pathLogFile);
currentLogFilesPath = logFileInfo.Directory.FullName;
logFileWithReferences = logFileInfo.FullName;
}
if (!File.Exists(logFileWithReferences))
logFileWithReferences = string.Format("{0}{1}{2}", pathLogFile, Path.DirectorySeparatorChar, @"1Cv8.lgd");
if (File.Exists(logFileWithReferences))
{
if (logFileInfo == null) logFileInfo = new FileInfo(logFileWithReferences);
string logFileExtension = logFileInfo.Extension.ToUpper();
if (logFileExtension.EndsWith("LGF"))
return new EventLogLGFReader(logFileInfo.FullName);
else if (logFileExtension.EndsWith("LGD"))
return new EventLogLGDReader(logFileInfo.FullName);
}
throw new ArgumentException("Invalid log file path");
}
protected string _logFilePath;
protected string _logFileDirectoryPath;
protected long _currentFileEventNumber;
public long CurrentFileEventNumber { get { return _currentFileEventNumber; } }
public string LogFilePath { get { return _logFilePath; } }
public string LogFileDirectoryPath { get { return _logFileDirectoryPath; } }
protected List<Applications> _applications;
protected List<Computers> _computers;
protected List<Metadata> _metadata;
protected List<Events> _events;
protected List<PrimaryPorts> _primaryPorts;
protected List<SecondaryPorts> _secondaryPorts;
protected List<Users> _users;
protected List<WorkServers> _workServers;
public delegate void BeforeReadFileHandler(EventLogReader sender, BeforeReadFileEventArgs args);
public delegate void AfterReadFileHandler(EventLogReader sender, AfterReadFileEventArgs args);
public delegate void BeforeReadEventHandler(EventLogReader sender, BeforeReadEventArgs args);
public delegate void AfterReadEventHandler(EventLogReader sender, AfterReadEventArgs args);
public delegate void OnErrorEventHandler(EventLogReader sender, OnErrorEventArgs args);
public event BeforeReadFileHandler BeforeReadFile;
public event AfterReadFileHandler AfterReadFile;
public event BeforeReadEventHandler BeforeReadEvent;
public event AfterReadEventHandler AfterReadEvent;
public event OnErrorEventHandler OnErrorEvent;
protected void RaiseBeforeReadFile(BeforeReadFileEventArgs args)
{
BeforeReadFile?.Invoke(this, args);
}
protected void RaiseAfterReadFile(AfterReadFileEventArgs args)
{
AfterReadFile?.Invoke(this, args);
}
protected void RaiseBeforeRead(BeforeReadEventArgs args)
{
BeforeReadEvent?.Invoke(this, args);
}
protected void RaiseAfterRead(AfterReadEventArgs args)
{
AfterReadEvent?.Invoke(this, args);
}
protected void RaiseOnError(OnErrorEventArgs args)
{
OnErrorEvent?.Invoke(this, args);
}
internal EventLogReader() : base() { }
internal EventLogReader(string logFilePath)
{
_logFilePath = logFilePath;
_logFileDirectoryPath = new FileInfo(_logFilePath).Directory.FullName;
_applications = new List<Applications>();
_computers = new List<Computers>();
_metadata = new List<Metadata>();
_events = new List<Events>();
_primaryPorts = new List<PrimaryPorts>();
_secondaryPorts = new List<SecondaryPorts>();
_users = new List<Users>();
_workServers = new List<WorkServers>();
ReadEventLogReferences();
}
public IReadOnlyList<Applications> Applications { get { return _applications; } }
public IReadOnlyList<Computers> Computers { get { return _computers; } }
public IReadOnlyList<Metadata> Metadata { get { return _metadata; } }
public IReadOnlyList<Events> Events { get { return _events; } }
public IReadOnlyList<PrimaryPorts> PrimaryPorts { get { return _primaryPorts; } }
public IReadOnlyList<SecondaryPorts> SecondaryPorts { get { return _secondaryPorts; } }
public IReadOnlyList<Users> Users { get { return _users; } }
public IReadOnlyList<WorkServers> WorkServers { get { return _workServers; } }
protected virtual void ReadEventLogReferences() { }
public virtual bool Read(out EventLogRowData rowData)
{
throw new NotImplementedException();
}
public virtual bool GoToEvent(long eventNumber)
{
throw new NotImplementedException();
}
public virtual EventLogPosition GetCurrentPosition()
{
throw new NotImplementedException();
}
public virtual void SetCurrentPosition(EventLogPosition newPosition)
{
throw new NotImplementedException();
}
public virtual long Count()
{
throw new NotImplementedException();
}
public virtual void Reset()
{
throw new NotImplementedException();
}
public virtual void NextFile()
{
throw new NotImplementedException();
}
public virtual void Dispose()
{
_applications.Clear();
_computers.Clear();
_metadata.Clear();
_events.Clear();
_primaryPorts.Clear();
_secondaryPorts.Clear();
_users.Clear();
_workServers.Clear();
}
public sealed class BeforeReadFileEventArgs : EventArgs
{
public BeforeReadFileEventArgs(string fileName)
{
FileName = fileName;
Cancel = false;
}
public string FileName { get; }
public bool Cancel { get; set; }
}
public sealed class AfterReadFileEventArgs : EventArgs
{
public AfterReadFileEventArgs(string fileName)
{
FileName = fileName;
}
public string FileName { get; }
}
public sealed class BeforeReadEventArgs : EventArgs
{
public BeforeReadEventArgs(string sourceData, long eventNumber)
{
SourceData = sourceData;
EventNumber = eventNumber;
}
public string SourceData { get; }
public long EventNumber { get; }
}
public sealed class AfterReadEventArgs : EventArgs
{
public AfterReadEventArgs(EventLogRowData rowData, long eventNumber)
{
RowData = rowData;
EventNumber = eventNumber;
}
public EventLogRowData RowData { get; }
public long EventNumber { get; }
}
public sealed class OnErrorEventArgs : EventArgs
{
public OnErrorEventArgs(Exception excepton, string sourceData, bool critical)
{
Exception = excepton;
SourceData = sourceData;
Critical = critical;
}
public Exception Exception { get; }
public string SourceData { get; }
public bool Critical { get; }
}
}
}

View File

@ -0,0 +1,18 @@
using System;
using System.Collections.Generic;
using System.Text;
using YY.LogReader.Models.EventLog;
namespace YY.LogReader.EventLog
{
internal interface IEventLogReader
{
bool Read(out EventLogRowData rowData);
bool GoToEvent(long eventNumber);
EventLogPosition GetCurrentPosition();
void SetCurrentPosition(EventLogPosition newPosition);
long Count();
void Reset();
void NextFile();
}
}

View File

@ -0,0 +1,243 @@
using YY.LogReader.Models.EventLog;
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using YY.LogReader.Services;
using System.Text.RegularExpressions;
using System.Data.SQLite;
namespace YY.LogReader.EventLog
{
internal sealed class LogParserLGF
{
private EventLogLGFReader _reader;
private Regex regexDataUUID;
public LogParserLGF(EventLogLGFReader reader)
{
_reader = reader;
regexDataUUID = new Regex(@"[\d]+:[\dA-Za-zА-Яа-я]{32}}");
}
public EventLogRowData Parse(string eventSource)
{
var parseResult = ParseEventLogString(eventSource);
DateTime eventDate = DateTime.ParseExact(parseResult[0], "yyyyMMddHHmmss", CultureInfo.InvariantCulture);
EventLogRowData eventData = new EventLogRowData();
eventData.RowID = _reader.CurrentFileEventNumber;
eventData.Period = eventDate;
string TransStr = parseResult[2].ToString().Replace("}", "").Replace("{", "");
long TransDate = TransStr.Substring(0, TransStr.IndexOf(",")).From16To10();
try
{
if (!(TransDate == 0))
eventData.TransactionDate = new System.DateTime().AddSeconds((double)TransDate / 10000);
}
catch
{
eventData.TransactionDate = null;
}
eventData.TransactionId = TransStr.Substring(TransStr.IndexOf(",") + 1).From16To10();
if (parseResult[1] == "R")
eventData.TransactionStatus = TransactionStatus.Unfinished;
else if (parseResult[1] == "N")
eventData.TransactionStatus = TransactionStatus.NotApplicable;
else if (parseResult[1] == "U")
eventData.TransactionStatus = TransactionStatus.Committed;
else if (parseResult[1] == "C")
eventData.TransactionStatus = TransactionStatus.RolledBack;
else
eventData.TransactionStatus = TransactionStatus.Unknown;
eventData.Comment = parseResult[9].RemoveQuotes();
eventData.Session = Convert.ToInt32(parseResult[16]);
eventData.ConnectId = Convert.ToInt32(parseResult[6]);
eventData.DataPresentation = parseResult[12].RemoveQuotes();
eventData.Data = parseResult[11];
MatchCollection matches = regexDataUUID.Matches(eventData.Data);
if (matches.Count > 0)
{
string[] dataPartsUUID = eventData.Data.Split(':');
if (dataPartsUUID.Length == 2)
{
string dataUUID = dataPartsUUID[1].Replace("}", string.Empty);
eventData.DataUUID = dataUUID;
}
}
long userID = Convert.ToInt64(parseResult[3]);
eventData.User = _reader.Users.Where(i => i.Code == userID).FirstOrDefault();
long computerID = Convert.ToInt64(parseResult[4]);
eventData.Computer = _reader.Computers.Where(i => i.Code == computerID).FirstOrDefault();
long appID = Convert.ToInt64(parseResult[5]);
eventData.Application = _reader.Applications.Where(i => i.Code == appID).FirstOrDefault();
long eventID = Convert.ToInt64(parseResult[7]);
eventData.Event = _reader.Events.Where(i => i.Code == eventID).FirstOrDefault();
long metadataID = Convert.ToInt64(parseResult[10]);
eventData.Metadata = _reader.Metadata.Where(i => i.Code == metadataID).FirstOrDefault();
long workServerID = Convert.ToInt64(parseResult[13]);
eventData.WorkServer = _reader.WorkServers.Where(i => i.Code == workServerID).FirstOrDefault();
long pimaryPortID = Convert.ToInt64(parseResult[14]);
eventData.PrimaryPort = _reader.PrimaryPorts.Where(i => i.Code == pimaryPortID).FirstOrDefault();
long secondaryPortID = Convert.ToInt64(parseResult[15]);
eventData.SecondaryPort = _reader.SecondaryPorts.Where(i => i.Code == secondaryPortID).FirstOrDefault();
if (eventData.Data == "{\"U\"}") // 'empty reference
eventData.Data = string.Empty;
else if (eventData.Data.StartsWith("{"))
{
//'internal representation for different objects.
var ParsedObject = ParseEventLogString(eventData.Data);
if (ParsedObject.Length == 2)
{
if (ParsedObject[0] == "\"S\"" || ParsedObject[0] == "\"R\"")
{
//'this is string or reference
eventData.Data = ParsedObject[1].RemoveQuotes(); // 'string value
}
}
}
switch (parseResult[8].Trim())
{
case "I":
eventData.Severity = Severity.Information;
break;
case "W":
eventData.Severity = Severity.Warning;
break;
case "E":
eventData.Severity = Severity.Error;
break;
case "N":
eventData.Severity = Severity.Note;
break;
default:
eventData.Severity = Severity.Unknown;
break;
}
return eventData;
}
internal bool ItsEndOfEvent(string Str, ref int Count, ref bool TextBlockOpen)
{
string TempStr = Str;
for (int i = 0; i <= TempStr.Length - 1; i++)
{
string Simb = TempStr.Substring(i, 1);
if (Simb == "\"")
{
TextBlockOpen = !TextBlockOpen;
}
else if (Simb == "}" & !TextBlockOpen)
{
Count = Count - 1;
}
else if (Simb == "{" & !TextBlockOpen)
{
Count = Count + 1;
}
}
return (Count == 0);
}
internal string[] ParseEventLogString(string Text)
{
string[] ArrayLines = null;
string Text2 = Text.Substring(1, (Text.EndsWith(",") ? Text.Length - 3 : Text.Length - 2)) + ",";
string Str = "";
int Delim = Text2.IndexOf(",");
int i = 0;
int partNumber = 0;
bool isSpecialString = false;
while (Delim > 0)
{
Str = Str + Text2.Substring(0, Delim).Trim();
partNumber += 1;
Text2 = Text2.Substring(Delim + 1);
if (partNumber == 1 && !String.IsNullOrEmpty(Str) && Str[0] == '\"')
isSpecialString = true;
int count1;
int count2;
if (isSpecialString)
{
count1 = 0;
count2 = 0;
}
else
{
count1 = CountSubstringInString(Str, "{");
count2 = CountSubstringInString(Str, "}");
}
int count3 = CountSubstringInString(Str, "\"") % 2; // Math.IEEERemainder(CountSubstringInString(Str, "\""), 2);
if (count1 == count2 & count3 == 0)
{
Array.Resize(ref ArrayLines, i + 1);
if (Str.StartsWith("\"") && Str.EndsWith("\""))
{
Str = Str.Substring(1, Str.Length - 2);
}
if (isSpecialString)
{
char[] denied = new[] { '\n', '\t', '\r' };
StringBuilder newString = new StringBuilder();
foreach (var ch in Str)
if (!denied.Contains(ch))
newString.Append(ch);
Str = newString.ToString();
}
ArrayLines[i] = Str;
i = i + 1;
Str = "";
partNumber = 0;
isSpecialString = false;
}
else
{
Str = Str + ",";
}
if (isSpecialString) // Особая обработка для поля "DataPresentation"
{
Delim = Text2.IndexOf("\",") + 1;
}
else
{
Delim = Text2.IndexOf(",");
}
}
return ArrayLines;
}
private int CountSubstringInString(string Str, string SubStr)
{
return (Str.Length - Str.Replace(SubStr, "").Length) / SubStr.Length;
}
}
}

View File

@ -0,0 +1,19 @@
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
namespace YY.LogReader.Models.EventLog
{
public class Applications
{
[Key]
public long Code { get; set; }
[MaxLength(250)]
public string Name { get; set; }
public override string ToString()
{
return Name;
}
}
}

View File

@ -0,0 +1,19 @@
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
namespace YY.LogReader.Models.EventLog
{
public class Computers
{
[Key]
public long Code { get; set; }
[MaxLength(250)]
public string Name { get; set; }
public override string ToString()
{
return Name;
}
}
}

View File

@ -0,0 +1,29 @@
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
namespace YY.LogReader.Models.EventLog
{
public class EventLogRowData : RowData
{
public long Id { get; set; }
public Severity Severity { get; set; }
public long? ConnectId { get; set; }
public long? Session { get; set; }
public TransactionStatus TransactionStatus { get; set; }
public DateTime? TransactionDate { get; set; }
public long? TransactionId { get; set; }
public Users User { get; set; }
public Computers Computer { get; set; }
public Applications Application { get; set; }
public Events Event { get; set; }
public string Comment { get; set; }
public Metadata Metadata { get; set; }
public string Data { get; set; }
public string DataUUID { get; set; }
public string DataPresentation { get; set; }
public WorkServers WorkServer { get; set; }
public PrimaryPorts PrimaryPort { get; set; }
public SecondaryPorts SecondaryPort { get; set; }
}
}

View File

@ -0,0 +1,19 @@
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
namespace YY.LogReader.Models.EventLog
{
public class Events
{
[Key]
public long Code { get; set; }
[MaxLength(250)]
public string Name { get; set; }
public override string ToString()
{
return Name;
}
}
}

View File

@ -0,0 +1,20 @@
using System;
using System.Collections.Generic;
using System.Text;
using System.ComponentModel.DataAnnotations;
namespace YY.LogReader.Models.EventLog
{
public class InformationSystems
{
[Key]
public long Code { get; set; }
[MaxLength(250)]
public string Name { get; set; }
public override string ToString()
{
return Name;
}
}
}

View File

@ -0,0 +1,20 @@
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
namespace YY.LogReader.Models.EventLog
{
public class Metadata
{
[Key]
public long Code { get; set; }
public Guid Uuid { get; set; }
[MaxLength(250)]
public string Name { get; set; }
public override string ToString()
{
return Name;
}
}
}

View File

@ -0,0 +1,19 @@
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
namespace YY.LogReader.Models.EventLog
{
public class PrimaryPorts
{
[Key]
public long Code { get; set; }
[MaxLength(250)]
public string Name { get; set; }
public override string ToString()
{
return Name;
}
}
}

View File

@ -0,0 +1,24 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using YY.LogReader.Models.EventLog;
namespace YY.LogReader.Models
{
public abstract class RowData
{
[Key]
[Column(Order = 1)]
public InformationSystems InformationSystem { get; set; }
[Key]
[Column(Order = 2)]
public DateTimeOffset Period { get; set; }
[Key]
[Column(Order = 3)]
public long RowID { get; set; }
}
}

View File

@ -0,0 +1,19 @@
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
namespace YY.LogReader.Models.EventLog
{
public class SecondaryPorts
{
[Key]
public long Code { get; set; }
[MaxLength(250)]
public string Name { get; set; }
public override string ToString()
{
return Name;
}
}
}

View File

@ -0,0 +1,15 @@
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
namespace YY.LogReader.Models.EventLog
{
public enum Severity
{
Unknown = 0,
Information = 1,
Warning = 2,
Error = 3,
Note = 4
}
}

View File

@ -0,0 +1,15 @@
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
namespace YY.LogReader.Models.EventLog
{
public enum TransactionStatus
{
Unknown = 0,
Committed = 1,
Unfinished = 2,
NotApplicable = 3,
RolledBack = 4
}
}

View File

@ -0,0 +1,20 @@
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
namespace YY.LogReader.Models.EventLog
{
public class Users
{
[Key]
public long Code { get; set; }
public Guid Uuid { get; set; }
[MaxLength(250)]
public string Name { get; set; }
public override string ToString()
{
return Name;
}
}
}

View File

@ -0,0 +1,19 @@
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
namespace YY.LogReader.Models.EventLog
{
public class WorkServers
{
[Key]
public long Code { get; set; }
[MaxLength(250)]
public string Name { get; set; }
public override string ToString()
{
return Name;
}
}
}

View File

@ -0,0 +1,30 @@
using System;
using System.Data.SqlTypes;
namespace YY.LogReader.Services
{
internal static class DateTimeExtensions
{
public static DateTime? ToNullIfTooEarlyForDb(this DateTime date)
{
return (date >= (DateTime)SqlDateTime.MinValue) ? date : (DateTime?)null;
}
public static DateTime ToMinDateTimeIfNull(this DateTime? date)
{
return (date == null) ? DateTime.MinValue : (DateTime)date;
}
public static long ToLongDateTimeFormat(this DateTime date)
{
return (long)(date - DateTime.MinValue).TotalMilliseconds * 10;
}
public static long ToMilliseconds(this DateTime date)
{
return (long)date.ToUniversalTime().Subtract(
new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc)
).TotalMilliseconds;
}
}
}

View File

@ -0,0 +1,20 @@
using System;
namespace YY.LogReader.Services
{
internal static class IntExtensions
{
public static DateTime ToDateTimeFormat(this long s)
{
return DateTime.MinValue.AddSeconds((double)s / 10000);
}
public static DateTime? ToNullableDateTimeELFormat(this long s)
{
if (s == 0)
return null;
else
return DateTime.MinValue.AddSeconds((double)s / 10000);
}
}
}

View File

@ -0,0 +1,53 @@
using System;
using System.Data.SQLite;
using System.Text;
namespace YY.LogReader.Services
{
internal static class SQLiteExtensions
{
public static string GetStringOrDefault(this SQLiteDataReader reader, int valueIndex)
{
try
{
return reader.GetString(valueIndex);
}
catch
{
return string.Empty;
}
}
public static long GetInt64OrDefault(this SQLiteDataReader reader, int valueIndex)
{
try
{
return reader.GetInt64(valueIndex);
}
catch
{
return 0;
}
}
public static string GetRowAsString(this SQLiteDataReader reader)
{
StringBuilder builder = new StringBuilder();
for (int i = 0; i < reader.FieldCount; i++)
{
builder.Append(reader.GetName(i));
builder.Append(" : ");
builder.Append(Convert.ToString(reader.GetValue(i)));
builder.AppendLine();
}
return builder.ToString();
}
public static string GetConnectionString(string dbFile)
{
return String.Format("Data Source={0};Version=3;Read Only=True;", dbFile);
}
}
}

View File

@ -0,0 +1,105 @@
using System;
using System.IO;
using System.Text;
namespace YY.LogReader.Services
{
internal class StreamLineReader : IDisposable
{
const int BufferLength = 1024;
Stream _Base;
int _Read = 0, _Index = 0;
byte[] _Bff = new byte[BufferLength];
long _CurrentPosition = 0;
long _CurrentLine = 0;
public long CurrentPosition { get { return _CurrentPosition; } }
public long CurrentLine { get { return _CurrentLine; } }
public StreamLineReader(Stream stream) { _Base = stream; }
public bool GoToLine(long goToLine) { return IGetCount(goToLine, true) == goToLine; }
public long GetCount(long goToLine) { return IGetCount(goToLine, false); }
long IGetCount(long goToLine, bool stopWhenLine)
{
_Base.Seek(0, SeekOrigin.Begin);
_CurrentPosition = 0;
_CurrentLine = 0;
_Index = 0;
_Read = 0;
long savePosition = _Base.Length;
do
{
if (_CurrentLine == goToLine)
{
savePosition = _CurrentPosition;
if (stopWhenLine) return _CurrentLine;
}
}
while (ReadLine() != null);
long count = _CurrentLine;
_CurrentLine = goToLine;
_Base.Seek(savePosition, SeekOrigin.Begin);
return count;
}
public string ReadLine()
{
bool found = false;
StringBuilder sb = new StringBuilder();
while (!found)
{
if (_Read <= 0)
{
// Read next block
_Index = 0;
_Read = _Base.Read(_Bff, 0, BufferLength);
if (_Read == 0)
{
if (sb.Length > 0) break;
return null;
}
}
for (int max = _Index + _Read; _Index < max; )
{
char ch = (char)_Bff[_Index];
_Read--; _Index++;
_CurrentPosition++;
if (ch == '\0' || ch == '\n')
{
found = true;
break;
}
else if (ch == '\r') continue;
else sb.Append(ch);
}
}
_CurrentLine++;
return sb.ToString();
}
public void Dispose()
{
if (_Base != null)
{
_Base.Close();
_Base.Dispose();
_Base = null;
}
}
}
}

View File

@ -0,0 +1,44 @@
using System.IO;
using System.Reflection;
namespace YY.LogReader.Services
{
internal static class StreamReaderExtensions
{
readonly static FieldInfo charPosField = typeof(StreamReader).GetField("_charPos", BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.DeclaredOnly);
readonly static FieldInfo byteLenField = typeof(StreamReader).GetField("_byteLen", BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.DeclaredOnly);
readonly static FieldInfo charBufferField = typeof(StreamReader).GetField("_charBuffer", BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.DeclaredOnly);
public static long GetPosition(this StreamReader reader)
{
int byteLen = (int)byteLenField.GetValue(reader);
var position = reader.BaseStream.Position - byteLen;
int charPos = (int)charPosField.GetValue(reader);
if (charPos > 0)
{
var charBuffer = (char[])charBufferField.GetValue(reader);
var encoding = reader.CurrentEncoding;
var bytesConsumed = encoding.GetBytes(charBuffer, 0, charPos).Length;
position += bytesConsumed;
}
return position;
}
public static void SetPosition(this StreamReader reader, long position)
{
reader.DiscardBufferedData();
reader.BaseStream.Seek(position, SeekOrigin.Begin);
}
public static void SkipLine(this StreamReader stream, long numberToSkip)
{
for (int i = 0; i < numberToSkip; i++)
{
stream.ReadLine();
}
}
}
}

View File

@ -0,0 +1,216 @@
using System;
using System.Collections.Generic;
using System.Security.Cryptography;
using System.Text;
namespace YY.LogReader.Services
{
internal static class StringExtensions
{
public static long From16To10(this string Str)
{
return Convert.ToInt64(Str.ToUpper(), 16);
}
public static string RemoveQuotes(this string s)
{
string functionReturnValue = s;
if (functionReturnValue.StartsWith("\""))
{
functionReturnValue = functionReturnValue.Substring(1);
}
if (functionReturnValue.EndsWith("\""))
{
functionReturnValue = functionReturnValue.Substring(0, functionReturnValue.Length - 1);
}
return functionReturnValue;
}
public static string ConvertEncoding(this string s, Encoding source, Encoding result)
{
byte[] souceBytes = source.GetBytes(s);
byte[] resultBytes = Encoding.Convert(source, result, souceBytes);
return result.GetString(resultBytes);
//return result.GetString(source.GetBytes(s));
}
public static string FromAnsiToUTF8(this string s)
{
return ConvertEncoding(s,
Encoding.GetEncoding(1252),
Encoding.UTF8);
}
public static string FromWIN1251ToUTF8(this string s)
{
Encoding win1251 = CodePagesEncodingProvider.Instance.GetEncoding(1251);
return ConvertEncoding(s, win1251, Encoding.UTF8);
}
public static string FromWIN1252ToUTF8(this string s)
{
return ConvertEncoding(s,
Encoding.GetEncoding("windows-1252"),
Encoding.UTF8);
}
public static Guid ToGuid(this string s)
{
Guid guidFromString = Guid.Empty;
Guid.TryParse(s, out guidFromString);
return guidFromString;
}
public static bool IsLike(this string s, string pattern)
{
// Characters matched so far
int matched = 0;
// Loop through pattern string
for (int i = 0; i < pattern.Length; )
{
// Check for end of string
if (matched > s.Length)
return false;
// Get next pattern character
char c = pattern[i++];
if (c == '[') // Character list
{
// Test for exclude character
bool exclude = (i < pattern.Length && pattern[i] == '!');
if (exclude)
i++;
// Build character list
int j = pattern.IndexOf(']', i);
if (j < 0)
j = s.Length;
HashSet<char> charList = CharListToSet(pattern.Substring(i, j - i));
i = j + 1;
if (charList.Contains(s[matched]) == exclude)
return false;
matched++;
}
else if (c == '?') // Any single character
{
matched++;
}
else if (c == '#') // Any single digit
{
if (!Char.IsDigit(s[matched]))
return false;
matched++;
}
else if (c == '*') // Zero or more characters
{
if (i < pattern.Length)
{
// Matches all characters until
// next character in pattern
char next = pattern[i];
int j = s.IndexOf(next, matched);
if (j < 0)
return false;
matched = j;
}
else
{
// Matches all remaining characters
matched = s.Length;
break;
}
}
else // Exact character
{
if (matched >= s.Length || c != s[matched])
return false;
matched++;
}
}
// Return true if all characters matched
return (matched == s.Length);
}
private static HashSet<char> CharListToSet(string charList)
{
HashSet<char> set = new HashSet<char>();
for (int i = 0; i < charList.Length; i++)
{
if ((i + 1) < charList.Length && charList[i + 1] == '-')
{
// Character range
char startChar = charList[i++];
i++; // Hyphen
char endChar = (char)0;
if (i < charList.Length)
endChar = charList[i++];
for (int j = startChar; j <= endChar; j++)
set.Add((char)j);
}
else set.Add(charList[i]);
}
return set;
}
public static string GetHashMD5(this string input)
{
MD5 md5 = System.Security.Cryptography.MD5.Create();
byte[] inputBytes = System.Text.Encoding.ASCII.GetBytes(input);
byte[] hash = md5.ComputeHash(inputBytes);
StringBuilder sb = new StringBuilder();
for (int i = 0; i < hash.Length; i++)
sb.Append(hash[i].ToString("X2"));
return sb.ToString();
}
private static readonly Dictionary<int, char> CharactersToMap = new Dictionary<int, char>
{
{130, '‚'},
{131, 'ƒ'},
{132, '„'},
{133, '…'},
{134, '†'},
{135, '‡'},
{136, 'ˆ'},
{137, '‰'},
{138, 'Š'},
{139, '‹'},
{140, 'Œ'},
{145, '‘'},
{146, '’'},
{147, '“'},
{148, '”'},
{149, '•'},
{150, '–'},
{151, '—'},
{152, '˜'},
{153, '™'},
{154, 'š'},
{155, '›'},
{156, 'œ'},
{159, 'Ÿ'},
{173, '-'}
};
public static string ConvertFromWindowsToUnicode(this string txt)
{
Encoding utf8 = Encoding.GetEncoding("utf-8");
Encoding win1251 = Encoding.GetEncoding("windows-1252");
byte[] utf8Bytes = win1251.GetBytes(txt);
byte[] win1251Bytes = Encoding.Convert(win1251, utf8, utf8Bytes);
string result = win1251.GetString(win1251Bytes);
return result;
}
}
}

View File

@ -0,0 +1,18 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
</PropertyGroup>
<ItemGroup>
<Folder Include="Services\" />
<Folder Include="Models\" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="System.ComponentModel.Annotations" Version="4.7.0" />
<PackageReference Include="System.Data.SQLite" Version="1.0.112" />
<PackageReference Include="System.Text.Encoding.CodePages" Version="4.7.0" />
</ItemGroup>
</Project>