2using System.Collections.Generic;
4using System.Reflection;
5using System.Security.Cryptography;
6using System.Threading.Tasks;
18 internal const string Preamble =
"IoT Gateway Export";
150 private BinaryWriter w;
151 private Stream output;
152 private CryptoStream cs;
153 private readonly
int blockSize;
154 private LinkedList<string> errors =
null;
155 private LinkedList<Exception> exceptions =
null;
156 private bool exportCollection =
false;
168 : this(
FileName,
Created, Output, File, null, 0, OnlySelectedCollections, SelectedCollections)
184 bool OnlySelectedCollections, Array SelectedCollections)
185 : base(
FileName,
Created, File, OnlySelectedCollections, SelectedCollections)
187 this.output = Output;
188 this.w =
new BinaryWriter(this.output, System.Text.Encoding.UTF8);
189 this.cs = CryptoStream;
190 this.blockSize = BlockSize;
196 if (!(this.w is
null))
201 if (!(this.cs is
null) && this.blockSize > 1)
203 Type T = typeof(CryptoStream);
207 PropertyInfo PI = T.GetProperty(
"_inputBufferIndex", BindingFlags.Instance | BindingFlags.NonPublic);
210 FI = T.GetField(
"_inputBufferIndex", BindingFlags.Instance | BindingFlags.NonPublic);
213 PI = T.GetProperty(
"_InputBufferIndex", BindingFlags.Instance | BindingFlags.NonPublic);
215 FI = T.GetField(
"_InputBufferIndex", BindingFlags.Instance | BindingFlags.NonPublic);
222 i = (int)PI.GetValue(
this.cs);
223 else if (!(FI is
null))
224 i = (
int)FI.GetValue(this.cs);
230 this.w.Write((
byte)0);
237 this.cs.FlushFinalBlock();
244 this.output?.Dispose();
258 this.w.Write((
byte)1);
259 this.w.Write(Preamble);
260 return Task.FromResult(
true);
267 public override Task<bool>
End()
269 if (!(this.errors is
null))
271 this.w.Write((
byte)4);
273 foreach (
string Message
in this.errors)
274 this.w.Write(Message);
276 this.w.Write(
string.Empty);
280 if (!(this.exceptions is
null))
282 this.w.Write((
byte)5);
284 foreach (Exception ex
in this.exceptions)
285 this.OutputException(ex);
287 this.w.Write(
string.Empty);
288 this.exceptions =
null;
291 this.w.Write((
byte)0);
295 private Task OutputException(Exception Exception)
297 this.w.Write(Exception.Message);
300 if (Exception is AggregateException AggregateException)
302 foreach (Exception ex
in AggregateException.InnerExceptions)
303 this.OutputException(ex);
305 else if (!(Exception.InnerException is
null))
306 this.OutputException(Exception.InnerException);
308 this.w.Write(
string.Empty);
310 return Task.CompletedTask;
318 this.w.Write((
byte)2);
319 return Task.FromResult(
true);
327 this.w.Write(
string.Empty);
337 this.w.Write((
byte)6);
338 return Task.FromResult(
true);
347 this.w.Write(
string.Empty);
359 this.w.Write(CollectionName);
361 return Task.FromResult(
true);
370 if (this.exportCollection)
371 this.w.Write((
byte)0);
373 return Task.FromResult(
true);
382 if (this.exportCollection)
383 this.w.Write((
byte)1);
385 return Task.FromResult(
true);
394 if (this.exportCollection)
395 this.w.Write(
string.Empty);
397 return Task.FromResult(
true);
408 if (this.exportCollection)
410 this.w.Write(FieldName);
411 this.w.Write(Ascending);
414 return Task.FromResult(
true);
424 if (this.exportCollection)
426 this.w.Write((
byte)1);
427 this.w.Write(BlockID);
430 return Task.FromResult(
true);
439 if (this.exportCollection)
440 this.w.Write((
byte)3);
442 return Task.FromResult(
true);
453 if (this.exportCollection)
455 this.w.Write((
byte)4);
469 public override Task<string>
StartObject(
string ObjectId,
string TypeName)
471 if (this.exportCollection)
473 this.w.Write((
byte)2);
474 this.w.Write(ObjectId);
475 this.w.Write(TypeName);
478 return Task.FromResult(ObjectId);
487 if (this.exportCollection)
490 this.w.Write(
string.Empty);
506 if (this.exportCollection)
508 this.w.Write((
byte)2);
509 this.w.Write(ObjectId);
510 this.w.Write(TypeName);
513 DateTime DT = EntryTimestamp.DateTime;
514 TimeSpan TS = EntryTimestamp.Offset;
516 this.w.Write((
byte)DT.Kind);
517 this.w.Write(DT.Ticks);
518 this.w.Write(TS.Ticks);
521 return Task.FromResult(
true);
530 if (this.exportCollection)
533 this.w.Write(
string.Empty);
545 public override Task<bool>
ReportProperty(
string PropertyName,
object PropertyValue)
547 if (this.exportCollection)
549 if (PropertyValue is
null)
552 if (!(PropertyName is
null))
553 this.w.Write(PropertyName);
555 else if (PropertyValue is Enum)
558 if (!(PropertyName is
null))
559 this.w.Write(PropertyName);
560 this.w.Write(PropertyValue.ToString());
564 switch (Type.GetTypeCode(PropertyValue.GetType()))
566 case TypeCode.Boolean:
568 if (!(PropertyName is
null))
569 this.w.Write(PropertyName);
570 this.w.Write((
bool)PropertyValue);
575 if (!(PropertyName is
null))
576 this.w.Write(PropertyName);
577 this.w.Write((
byte)PropertyValue);
582 if (!(PropertyName is
null))
583 this.w.Write(PropertyName);
584 this.w.Write((
char)PropertyValue);
587 case TypeCode.DateTime:
589 if (!(PropertyName is
null))
590 this.w.Write(PropertyName);
592 DateTime DT = (DateTime)PropertyValue;
594 this.w.Write((
byte)DT.Kind);
595 this.w.Write(DT.Ticks);
598 case TypeCode.Decimal:
600 if (!(PropertyName is
null))
601 this.w.Write(PropertyName);
602 this.w.Write((decimal)PropertyValue);
605 case TypeCode.Double:
607 if (!(PropertyName is
null))
608 this.w.Write(PropertyName);
609 this.w.Write((
double)PropertyValue);
614 if (!(PropertyName is
null))
615 this.w.Write(PropertyName);
616 this.w.Write((
short)PropertyValue);
621 if (!(PropertyName is
null))
622 this.w.Write(PropertyName);
623 this.w.Write((
int)PropertyValue);
628 if (!(PropertyName is
null))
629 this.w.Write(PropertyName);
630 this.w.Write((
long)PropertyValue);
635 if (!(PropertyName is
null))
636 this.w.Write(PropertyName);
637 this.w.Write((sbyte)PropertyValue);
640 case TypeCode.Single:
642 if (!(PropertyName is
null))
643 this.w.Write(PropertyName);
644 this.w.Write((
float)PropertyValue);
647 case TypeCode.String:
649 if (!(PropertyName is
null))
650 this.w.Write(PropertyName);
651 this.w.Write((
string)PropertyValue);
654 case TypeCode.UInt16:
656 if (!(PropertyName is
null))
657 this.w.Write(PropertyName);
658 this.w.Write((ushort)PropertyValue);
661 case TypeCode.UInt32:
663 if (!(PropertyName is
null))
664 this.w.Write(PropertyName);
665 this.w.Write((uint)PropertyValue);
668 case TypeCode.UInt64:
670 if (!(PropertyName is
null))
671 this.w.Write(PropertyName);
672 this.w.Write((ulong)PropertyValue);
675 case TypeCode.DBNull:
678 if (!(PropertyName is
null))
679 this.w.Write(PropertyName);
682 case TypeCode.Object:
683 if (PropertyValue is TimeSpan TS)
686 if (!(PropertyName is
null))
687 this.w.Write(PropertyName);
688 this.w.Write(TS.Ticks);
690 else if (PropertyValue is DateTimeOffset DTO)
693 if (!(PropertyName is
null))
694 this.w.Write(PropertyName);
699 this.w.Write((
byte)DT.Kind);
700 this.w.Write(DT.Ticks);
701 this.w.Write(TS.Ticks);
706 if (!(PropertyName is
null))
707 this.w.Write(PropertyName);
708 this.w.Write(CiString.Value);
710 else if (PropertyValue is
byte[] Bin)
713 if (!(PropertyName is
null))
714 this.w.Write(PropertyName);
715 this.w.Write(Bin.Length);
718 else if (PropertyValue is Guid Id)
721 if (!(PropertyName is
null))
722 this.w.Write(PropertyName);
723 this.w.Write(Id.ToByteArray());
725 else if (PropertyValue is Array A)
728 if (!(PropertyName is
null))
729 this.w.Write(PropertyName);
730 this.w.Write(PropertyValue.GetType().GetElementType().FullName);
732 this.w.Write(A.LongLength);
733 foreach (
object Obj
in A)
739 if (!(PropertyName is
null))
740 this.w.Write(PropertyName);
741 this.w.Write(Obj.TypeName);
743 foreach (KeyValuePair<string, object> P
in Obj)
747 this.w.Write(
string.Empty);
750 throw new Exception(
"Unhandled property value type: " + PropertyValue.GetType().FullName);
754 throw new Exception(
"Unhandled property value type: " + PropertyValue.GetType().FullName);
759 return Task.FromResult(
true);
769 if (!
string.IsNullOrEmpty(Message))
771 if (this.errors is
null)
772 this.errors =
new LinkedList<string>();
774 this.errors.AddLast(Message);
777 return Task.FromResult(
true);
787 if (this.exceptions is
null)
788 this.exceptions =
new LinkedList<Exception>();
790 this.exceptions.AddLast(Exception);
792 return Task.FromResult(
true);
801 this.w.Write((
byte)3);
802 return Task.FromResult(
true);
811 this.w.Write(
string.Empty);
812 return Task.FromResult(
true);
824 this.w.Write(File.Length);
827 await File.CopyToAsync(this.output);
Static class managing the application event log. Applications and services log events on this static ...
static string CleanStackTrace(string StackTrace)
Cleans a Stack Trace string, removing entries from the asynchronous execution model,...
Represents a case-insensitive string.
Generic object. Contains a sequence of properties.
EntryType
Ledger entry type.