private static void WriteLine(LogArea logArea, string message) { lock (s_locker) { using (StreamWriter w = File.AppendText(BuildLogPath(logArea.ToString()))) { w.WriteLine(message); w.Flush(); w.Close(); } } }
public static void Log(LogArea logArea, LogType logType, string message, object data) { var connectionString = AppSettings.GetConnectionString("Entities"); var conn = new SqlConnection(connectionString); try { string messageStr = GeneralHelper.LimitLength(message, 250); if (messageStr == null) { throw new ArgumentException("Message cannot be null"); } string dataStr = string.Empty; if (data != null) { try { dataStr = JsonConvert.SerializeObject(data); } catch { } } var sqlInsert = @"INSERT INTO [dbo].[Logs] ([LogArea], [LogType], [Message], [Data], [Timestamp]) VALUES (@LogArea, @LogType, @Message, @Data, GETDATE())"; using (var command = new SqlCommand(sqlInsert, conn)) { conn.Open(); command.Parameters.Add(new SqlParameter("@LogArea", logArea.ToString())); command.Parameters.Add(new SqlParameter("@LogType", logType.ToString())); command.Parameters.Add(new SqlParameter("@Message", messageStr)); command.Parameters.Add(new SqlParameter("@Data", dataStr)); command.ExecuteNonQuery(); } } catch (Exception ex) { Console.Write(ex.Message); } finally { if (conn.State == System.Data.ConnectionState.Open) { conn.Close(); } } }
private static string[] BuildLogHeading(LogArea logArea) { List <string> list = new List <string> { "====================================================================", "Administration Log" }; try { list.Add(string.Format(CultureInfo.InvariantCulture, "DateTime : {0}", new object[] { DateTime.Now })); list.Add(string.Format(CultureInfo.InvariantCulture, "Area : {0}", new object[] { logArea.ToString() })); //list.Add(string.Format(CultureInfo.InvariantCulture, "IP : {0}", new object[] { httpContext.Features.Get<IHttpConnectionFeature>()?.RemoteIpAddress })); } catch (Exception exception) { list.Add(string.Format(CultureInfo.InvariantCulture, "Error retrieving environment information: ", new object[] { exception.Message })); } list.Add("===================================================================="); return(list.ToArray()); }
FolderPath LogDir(LogArea target) => RootLogDir + FolderName.Define(target.ToString().ToLower());
private static string[] BuildLogHeading(LogArea logArea) { List <string> list = new List <string> { "====================================================================", "Administration Log" }; try { list.Add(string.Format(CultureInfo.InvariantCulture, "DateTime : {0}", new object[] { DateTime.Now })); list.Add(string.Format(CultureInfo.InvariantCulture, "Area : {0}", new object[] { logArea.ToString() })); list.Add(string.Format(CultureInfo.InvariantCulture, "IP : {0}", new object[] { }));// System.Web.HttpContext.Current.Request.UserHostAddress } catch (Exception exception) { list.Add(string.Format(CultureInfo.InvariantCulture, "Error retrieving environment information: ", new object[] { exception.Message })); } list.Add("===================================================================="); return(list.ToArray()); }