/// <summary> /// Debugs the content to a file/attachment with file name/key identifier as debug level /// </summary> /// <typeparam name="TLogProvider">The log provider to act on</typeparam> /// <typeparam name="T">The object to be stored</typeparam> /// <param name="content">The content to be stored in the file/attachment</param> /// <param name="identifier">The file name/identifier of the file/attachment</param> /// <param name="customFormat">(Optional) the custom serializer format</param> /// <returns>True if the log provider exists in the providers list</returns> public static bool DebugTo <TLogProvider, T>( T content, string identifier, SerializerFormat customFormat = SerializerFormat.NONE) where TLogProvider : ILogProvider where T : class, new() { var type = typeof(TLogProvider); var provider = Providers.SingleOrDefault(p => type == p.GetType()); if (provider == null) { return(false); } var category = GetCategory(); if (Filters.Any(f => !f.Filter(type.FullName, LogLevel.DEBUG, category, string.Empty))) { return(false); } provider.Debug(category, content, identifier, customFormat); return(true); }
/// <summary> /// Initializes a new instance of the <see cref="HttpClientService" /> class. /// </summary> /// <param name="format">序列化格式</param> public HttpClientService(SerializerFormat format) { this._serializer = this.GetSerialzar(format); this.MaxResponseContentBufferSize = int.MaxValue; this.Timeout = TimeSpan.FromSeconds(100); this.InitMediaTypeHeaderValue(format); }
/// <summary> /// Save the serializer version of <paramref name="content" /> in the file <paramref name="identifier" />, /// using default SerializerFormat, or a custom serializer format provided by <paramref name="customFormat" />. /// </summary> /// <typeparam name="T">The type of the parameter <paramref name="content" /></typeparam> /// <param name="content">The object/instance of a class to be serialized and saved in a disk file</param> /// <param name="identifier">The file name to be persisted to disk with the content</param> /// <param name="customFormat">Whatever or not to use a custom Serializer adapter different that one that is default for type</param> /// <remarks>Requires LogLevel.DEBUG flag</remarks> public void Debug <T>(T content, string identifier, SerializerFormat customFormat = SerializerFormat.NONE) where T : class { if (customFormat == SerializerFormat.NONE) { WriteInternal(LogLevel.DEBUG, (string)content.GetSerializer()); } else { WriteInternal(LogLevel.DEBUG, (string)content.GetCustomSerializer(customFormat)); } }
/// <summary> /// Converts the content type to a media type. /// </summary> /// <param name="serializerFormat">The serializer format.</param> /// <returns></returns> /// <exception cref="ArgumentOutOfRangeException">serializerFormat</exception> public static string ToMediaType(SerializerFormat serializerFormat) { switch (serializerFormat) { case SerializerFormat.JSON: return JsonContentType; case SerializerFormat.XML: return XmlContentType; default: throw new ArgumentOutOfRangeException("serializerFormat"); } }
/// <summary> /// 初始化MediaTypeWithQualityHeaderValue /// </summary> /// <param name="format">序列化的格式</param> private void InitMediaTypeHeaderValue(SerializerFormat format) { switch (format) { case SerializerFormat.Json: this.MediaTypeHeaderValue = new MediaTypeWithQualityHeaderValue("application/json"); break; case SerializerFormat.Xml: this.MediaTypeHeaderValue = new MediaTypeWithQualityHeaderValue("application/xml"); break; } }
/// <summary> /// 取得實作序列化的物件 /// </summary> /// <param name="format">序列化的格式</param> /// <returns>實作序列化物件</returns> private ISerializer GetSerialzar(SerializerFormat format) { switch (format) { case SerializerFormat.Json: return(JsonHelper.Instance); //case SerializerFormat.Xml: // return XmlHelper.Instance; default: throw new NotImplementedException("Serializer Format Not Implemented."); } }
/// <summary> /// Save the serializer version of <paramref name="content" /> in the file <paramref name="identifier" />, /// using default SerializerFormat, or a custom serializer format provided by <paramref name="customFormat" />. /// </summary> /// <typeparam name="T">The type of the parameter <paramref name="content" /></typeparam> /// <param name="content">The object/instance of a class to be serialized and saved in a disk file</param> /// <param name="identifier">The file name to be persisted to disk with the content</param> /// <param name="customFormat">Whatever or not to use a custom Serializer adapter different that one that is default for type</param> /// <remarks>Requires LogLevel.DEBUG flag</remarks> public void Debug <T>(T content, string identifier, SerializerFormat customFormat = SerializerFormat.NONE) where T : class { if (!_level.HasFlag(LogLevel.DEBUG)) { return; } var contentAsString = customFormat == SerializerFormat.NONE ? content.GetSerializer() : content.GetCustomSerializer(customFormat); Debug((string)contentAsString, identifier); }
/// <summary> /// Logs the message as a file/attachment with a file name/identifier with debug level using a custom serializer or default. /// </summary> /// <typeparam name="T">any class that can be serialized to the <paramref name="customFormat" /> serializer format</typeparam> /// <param name="category">The category</param> /// <param name="content">The object to be serialized</param> /// <param name="identifier">The filename/attachment identifier (file name or key)</param> /// <param name="customFormat">(Optional) the custom serializer format</param> public void Debug <T>(string category, T content, string identifier, SerializerFormat customFormat = SerializerFormat.NONE) where T : class, new() { if (!_level.HasFlag(LogLevel.DEBUG)) { return; } _adapter.Debug(identifier); if (customFormat == SerializerFormat.NONE) { _adapter.Debug((string)content.GetSerializer()); } else { _adapter.Debug((string)content.GetCustomSerializer(customFormat)); } }
/// <summary> /// Logs the message as a file/attachment with a file name/identifier with debug level using a custom serializer or default. /// </summary> /// <typeparam name="T">any class that can be serialized to the <paramref name="customFormat" /> serializer format</typeparam> /// <param name="category">The category</param> /// <param name="content">The object to be serialized</param> /// <param name="identifier">The filename/attachment identifier (file name or key)</param> /// <param name="customFormat">(Optional) the custom serializer format</param> public void Debug <T>(string category, T content, string identifier, SerializerFormat customFormat = SerializerFormat.NONE) where T : class, new() { if (!_level.HasFlag(LogLevel.DEBUG)) { return; } string serialized; if (customFormat == SerializerFormat.NONE) { serialized = (string)content.GetSerializer(); } else { serialized = (string)content.GetCustomSerializer(customFormat); } PropagateInternal(Serialize(LogLevel.DEBUG, category, serialized, identifier)); }
/// <inheritdoc /> public void Debug <T>(T content, string identifier, SerializerFormat customFormat = SerializerFormat.NONE) where T : class { throw new NotImplementedException(); }
public static SerializerConverter <T> GetCustomSerializer <T>(SerializerFormat format) where T : class { var obj = (T)Activator.CreateInstance(typeof(T)); return(GetCustomSerializer(obj, format)); }
public static SerializerConverter <T> GetCustomSerializer <T>(this T obj, SerializerFormat format) where T : class { return(GetSerializer(obj, new SerializerAttribute(format))); }
/// <summary> /// Initializes a new instance of the <see cref="SerializerAttribute"/> class. /// </summary> /// <param name="format">The format.</param> /// <param name="isStrict">if set to <c>true</c> [is strict].</param> public SerializerAttribute(SerializerFormat format = SerializerFormat.XML, bool isStrict = true) { Format = format; IsStrict = isStrict; }
/// <summary> /// Does nothing /// </summary> /// <typeparam name="T">Not used</typeparam> /// <param name="category">The category</param> /// <param name="content">Not used</param> /// <param name="identifier">Not used</param> /// <param name="customFormat">Not used</param> public void Debug <T>(string category, T content, string identifier, SerializerFormat customFormat = SerializerFormat.NONE) where T : class, new() { _adapter.Debug(content, identifier, customFormat); }
/// <summary> /// Logs the message as a file/attachment with a file name/identifier with debug level using a custom serializer or default. /// </summary> /// <typeparam name="T">any class that can be serialized to the <paramref name="customFormat"/> serializer format</typeparam> /// <param name="content">The object to be serialized</param> /// <param name="identifier">The filename/attachment identifier (file name or key)</param> /// <param name="customFormat">(Optional) the custom serializer format</param> public static void Debug <T>(T content, [Localizable(false)] string identifier, SerializerFormat customFormat = SerializerFormat.NONE) where T : class, new() { var category = GetCategory(); foreach (var provider in Providers.Where(p => Filters.All(f => f.Filter(p.GetType().FullName, LogLevel.DEBUG, category, string.Empty)))) { provider.Debug(category, content, identifier, customFormat); } }
/// <summary> /// Initializes a new instance of the <see cref="JSONSerializer"/> class. /// </summary> /// <param name="format">The format.</param> public JSONSerializer(SerializerFormat format) { this.format = format; }