4using System.Reflection;
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;
319 this.w.Write((
byte)2);
320 return Task.FromResult(
true);
328 this.w.Write(
string.Empty);
339 this.w.Write((
byte)6);
340 return Task.FromResult(
true);
349 this.w.Write(
string.Empty);
361 this.w.Write(CollectionName);
363 return Task.FromResult(
true);
372 if (this.exportCollection)
373 this.w.Write((
byte)0);
375 return Task.FromResult(
true);
384 if (this.exportCollection)
385 this.w.Write((
byte)1);
387 return Task.FromResult(
true);
396 if (this.exportCollection)
397 this.w.Write(
string.Empty);
399 return Task.FromResult(
true);
410 if (this.exportCollection)
412 this.w.Write(FieldName);
413 this.w.Write(Ascending);
416 return Task.FromResult(
true);
426 if (this.exportCollection)
428 this.w.Write((
byte)1);
429 this.w.Write(BlockID);
432 return Task.FromResult(
true);
441 if (this.exportCollection)
442 this.w.Write((
byte)3);
444 return Task.FromResult(
true);
455 if (this.exportCollection)
457 this.w.Write((
byte)4);
471 public override Task<string>
StartObject(
string ObjectId,
string TypeName)
473 if (this.exportCollection)
475 this.w.Write((
byte)2);
476 this.w.Write(ObjectId);
477 this.w.Write(TypeName);
480 return Task.FromResult(ObjectId);
489 if (this.exportCollection)
492 this.w.Write(
string.Empty);
508 if (this.exportCollection)
510 this.w.Write((
byte)2);
511 this.w.Write(ObjectId);
512 this.w.Write(TypeName);
515 DateTime DT = EntryTimestamp.DateTime;
516 TimeSpan TS = EntryTimestamp.Offset;
518 this.w.Write((
byte)DT.Kind);
519 this.w.Write(DT.Ticks);
520 this.w.Write(TS.Ticks);
523 return Task.FromResult(
true);
532 if (this.exportCollection)
535 this.w.Write(
string.Empty);
547 public override Task<bool>
ReportProperty(
string PropertyName,
object PropertyValue)
549 if (this.exportCollection)
551 if (PropertyValue is
null)
554 if (!(PropertyName is
null))
555 this.w.Write(PropertyName);
557 else if (PropertyValue is Enum)
560 if (!(PropertyName is
null))
561 this.w.Write(PropertyName);
562 this.w.Write(PropertyValue.ToString());
566 switch (Type.GetTypeCode(PropertyValue.GetType()))
568 case TypeCode.Boolean:
570 if (!(PropertyName is
null))
571 this.w.Write(PropertyName);
572 this.w.Write((
bool)PropertyValue);
577 if (!(PropertyName is
null))
578 this.w.Write(PropertyName);
579 this.w.Write((
byte)PropertyValue);
584 if (!(PropertyName is
null))
585 this.w.Write(PropertyName);
586 this.w.Write((
char)PropertyValue);
589 case TypeCode.DateTime:
591 if (!(PropertyName is
null))
592 this.w.Write(PropertyName);
594 DateTime DT = (DateTime)PropertyValue;
596 this.w.Write((
byte)DT.Kind);
597 this.w.Write(DT.Ticks);
600 case TypeCode.Decimal:
602 if (!(PropertyName is
null))
603 this.w.Write(PropertyName);
604 this.w.Write((decimal)PropertyValue);
607 case TypeCode.Double:
609 if (!(PropertyName is
null))
610 this.w.Write(PropertyName);
611 this.w.Write((
double)PropertyValue);
616 if (!(PropertyName is
null))
617 this.w.Write(PropertyName);
618 this.w.Write((
short)PropertyValue);
623 if (!(PropertyName is
null))
624 this.w.Write(PropertyName);
625 this.w.Write((
int)PropertyValue);
630 if (!(PropertyName is
null))
631 this.w.Write(PropertyName);
632 this.w.Write((
long)PropertyValue);
637 if (!(PropertyName is
null))
638 this.w.Write(PropertyName);
639 this.w.Write((sbyte)PropertyValue);
642 case TypeCode.Single:
644 if (!(PropertyName is
null))
645 this.w.Write(PropertyName);
646 this.w.Write((
float)PropertyValue);
649 case TypeCode.String:
651 if (!(PropertyName is
null))
652 this.w.Write(PropertyName);
653 this.w.Write((
string)PropertyValue);
656 case TypeCode.UInt16:
658 if (!(PropertyName is
null))
659 this.w.Write(PropertyName);
660 this.w.Write((ushort)PropertyValue);
663 case TypeCode.UInt32:
665 if (!(PropertyName is
null))
666 this.w.Write(PropertyName);
667 this.w.Write((uint)PropertyValue);
670 case TypeCode.UInt64:
672 if (!(PropertyName is
null))
673 this.w.Write(PropertyName);
674 this.w.Write((ulong)PropertyValue);
677 case TypeCode.DBNull:
680 if (!(PropertyName is
null))
681 this.w.Write(PropertyName);
684 case TypeCode.Object:
685 if (PropertyValue is TimeSpan TS)
688 if (!(PropertyName is
null))
689 this.w.Write(PropertyName);
690 this.w.Write(TS.Ticks);
692 else if (PropertyValue is DateTimeOffset DTO)
695 if (!(PropertyName is
null))
696 this.w.Write(PropertyName);
701 this.w.Write((
byte)DT.Kind);
702 this.w.Write(DT.Ticks);
703 this.w.Write(TS.Ticks);
708 if (!(PropertyName is
null))
709 this.w.Write(PropertyName);
710 this.w.Write(CiString.Value);
712 else if (PropertyValue is
byte[] Bin)
715 if (!(PropertyName is
null))
716 this.w.Write(PropertyName);
717 this.w.Write(Bin.Length);
720 else if (PropertyValue is Guid Id)
723 if (!(PropertyName is
null))
724 this.w.Write(PropertyName);
725 this.w.Write(Id.ToByteArray());
727 else if (PropertyValue is Array A)
730 if (!(PropertyName is
null))
731 this.w.Write(PropertyName);
732 this.w.Write(PropertyValue.GetType().GetElementType().FullName);
734 this.w.Write(A.LongLength);
735 foreach (
object Obj
in A)
741 if (!(PropertyName is
null))
742 this.w.Write(PropertyName);
743 this.w.Write(Obj.TypeName);
745 foreach (KeyValuePair<string, object> P
in Obj)
749 this.w.Write(
string.Empty);
752 throw new Exception(
"Unhandled property value type: " + PropertyValue.GetType().FullName);
756 throw new Exception(
"Unhandled property value type: " + PropertyValue.GetType().FullName);
761 return Task.FromResult(
true);
771 if (!
string.IsNullOrEmpty(Message))
773 if (this.errors is
null)
774 this.errors =
new LinkedList<string>();
776 this.errors.AddLast(Message);
779 return Task.FromResult(
true);
789 if (this.exceptions is
null)
790 this.exceptions =
new LinkedList<Exception>();
792 this.exceptions.AddLast(Exception);
794 return Task.FromResult(
true);
803 this.w.Write((
byte)3);
804 return Task.FromResult(
true);
813 this.w.Write(
string.Empty);
814 return Task.FromResult(
true);
826 this.w.Write(File.Length);
829 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.
Interface for database providers that can be plugged into the static Database class.
Interface for ledger providers that can be plugged into the static Ledger class.
EntryType
Ledger entry type.