Skip to content

Commit

Permalink
Add sources into a VS solution.
Browse files Browse the repository at this point in the history
  • Loading branch information
diev committed Aug 5, 2016
1 parent 29678ce commit ee5ec4d
Show file tree
Hide file tree
Showing 13 changed files with 979 additions and 421 deletions.
385 changes: 0 additions & 385 deletions Mailer.cs

This file was deleted.

22 changes: 22 additions & 0 deletions Mailer.sln
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 14
VisualStudioVersion = 14.0.25420.1
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Mailer", "Mailer\Mailer.csproj", "{4AFA638A-FA32-4BE6-9823-F5C0B94129D6}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{4AFA638A-FA32-4BE6-9823-F5C0B94129D6}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{4AFA638A-FA32-4BE6-9823-F5C0B94129D6}.Debug|Any CPU.Build.0 = Debug|Any CPU
{4AFA638A-FA32-4BE6-9823-F5C0B94129D6}.Release|Any CPU.ActiveCfg = Release|Any CPU
{4AFA638A-FA32-4BE6-9823-F5C0B94129D6}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
EndGlobal
168 changes: 168 additions & 0 deletions Mailer/Lib/App.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,168 @@
// Copyright (c) 2016 Dmitrii Evdokimov. All rights reserved.
// Licensed under the Apache License, Version 2.0.
// Source https://github.com/diev/

using System;
using System.Diagnostics;
using System.IO;
using System.Reflection;
using System.Text;

namespace Lib
{
/// <summary>
/// Properties of application
/// </summary>
class App
{
#region Info
/// <summary>
/// Файл приложения
/// </summary>
public static readonly string Exe = Assembly.GetCallingAssembly().Location;
/// <summary>
/// Путь к размещению файла приложения
/// </summary>
public static readonly string Dir = AppDomain.CurrentDomain.BaseDirectory;
/// <summary>
/// Файл записи лога приложения
/// </summary>
public static string Log = Path.ChangeExtension(App.Exe, string.Format("{0:yyyyMMdd}.log", DateTime.Now));

static Assembly assembly = Assembly.GetCallingAssembly();
static AssemblyName assemblyName = assembly.GetName();
public static readonly string Name = assemblyName.Name;

/// <summary>
/// Версия приложения вида 1.1.60805.0
/// </summary>
public static readonly Version Ver = assemblyName.Version;
/// <summary>
/// Дата версии приложения
/// </summary>
public static readonly DateTime Dated = DateTime.Parse(string.Format("201{0}-{1}-{2}", Ver.Build / 10000, Ver.Build % 10000 / 100, Ver.Build % 100));
/// <summary>
/// Строка названием и версией приложения
/// </summary>
public static readonly string Version = string.Format("{0} v{1}", Name, Ver); //Ver.ToString(2), Ver.Build, Ver.Revision

//public static readonly string attribute = (attribute == null) ? string.Empty : attribute;
static AssemblyDescriptionAttribute descriptionAttribute = AssemblyDescriptionAttribute.GetCustomAttribute(assembly, typeof(AssemblyDescriptionAttribute)) as AssemblyDescriptionAttribute;
public static readonly string Description = descriptionAttribute.Description;

static AssemblyCompanyAttribute companyAttribute = AssemblyCompanyAttribute.GetCustomAttribute(assembly, typeof(AssemblyCompanyAttribute)) as AssemblyCompanyAttribute;
/// <summary>
/// Компания разработчика приложения
/// </summary>
public static readonly string Company = companyAttribute.Company;

static AssemblyCopyrightAttribute copyrightAttribute = AssemblyCopyrightAttribute.GetCustomAttribute(assembly, typeof(AssemblyCopyrightAttribute)) as AssemblyCopyrightAttribute;
/// <summary>
/// Авторские права на приложение
/// </summary>
public static readonly string Copyright = copyrightAttribute.Copyright;
#endregion Info

#region Paths
/// <summary>
/// Checks if a directory exists. If not, tries to create.
/// </summary>
/// <param name="dir">A directory to check or create.</param>
/// <returns>Returns the checked or created directory.</returns>
public static string CheckDirectory(string dir)
{
if (!Directory.Exists(dir))
{
try
{
Directory.CreateDirectory(dir);
}
catch (DirectoryNotFoundException)
{
ExitError(2, "Ошибка создания директории " + dir);
//If wrong, creates in the App directory.
//Trace.TraceError("Директорию не создать:" + ex.Message);
//dir = Path.Combine(App.Dir, "_Recovery", Path.GetDirectoryName(dir));
//Directory.CreateDirectory(dir);
//Trace.TraceWarning("Recovery directory {0} created.", dir);
}
catch (IOException)
{
ExitError(2, "Нет диска для директории " + dir);
//Trace.TraceError("Сетевой диск не доступен: " + ex.Message);
//dir = Path.Combine(App.Dir, "_Recovery", Path.GetDirectoryName(dir));
//Directory.CreateDirectory(dir);
//Trace.TraceWarning("Recovery directory {0} created.", dir);
}
catch (Exception ex)
{
ExitError(2, "Другая ошибка создания директории " + dir +
Environment.NewLine + ex.Message);
}
}
return dir;
}
#endregion Paths

#region Exit
/// <summary>
/// Завершить приложение с указанными кодом и информационным сообщением
/// </summary>
/// <param name="code">Код завершения</param>
/// <param name="msg">Текст информации</param>
public static void ExitInformation(int code, string msg)
{
Trace.TraceInformation(ExitMessage(code, msg));
Exit(code);
}

/// <summary>
/// Завершить приложение с указанными кодом и предупреждающим сообщением
/// </summary>
/// <param name="code">Код завершения</param>
/// <param name="msg">Текст предупреждения</param>
public static void ExitWarning(int code, string msg)
{
Trace.TraceWarning(ExitMessage(code, msg));
Exit(code);
}

/// <summary>
/// Завершить приложение с указанными кодом и сообщением об ошибке
/// </summary>
/// <param name="code">Код завершения</param>
/// <param name="msg">Текст ошибки</param>
public static void ExitError(int code, string msg)
{
Trace.TraceError(ExitMessage(code, msg));
Exit(code);
}

/// <summary>
/// Завершить приложение с указанными кодом
/// </summary>
/// <param name="code">Код завершения</param>
public static void Exit(int code)
{
Trace.Close();
Environment.Exit(code);
}

public static string ExitMessage(int code, string msg)
{
StringBuilder sb = new StringBuilder();
if (code > 0)
{
sb.Append("Exit ");
sb.Append(code);
}
if (msg != null)
{
sb.Append(": ");
sb.Append(msg);
}
return sb.ToString();
}
#endregion Exit
}
}
97 changes: 97 additions & 0 deletions Mailer/Lib/AppTraceListener.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
// Copyright (c) 2016 Dmitrii Evdokimov. All rights reserved.
// Licensed under the Apache License, Version 2.0.
// Source https://github.com/diev/

using System;
using System.Diagnostics;
using System.IO;
using System.Text;

namespace Lib
{
class AppTraceListener : DefaultTraceListener
{
public static TraceSwitch TraceConsole = new TraceSwitch("TraceConsole", "Trace level for console in config", "Error");
public static TraceSwitch TraceLog = new TraceSwitch("TraceLog", "Trace level for log in config", "Verbose");

public AppTraceListener()
{
}

public AppTraceListener(string file)
{
base.LogFileName = file;

string path = Path.GetDirectoryName(file);
if (!Directory.Exists(path))
{
Directory.CreateDirectory(path);
}
}

public override void TraceEvent(TraceEventCache eventCache, string source, TraceEventType eventType, int id, string format, params object[] args)
{
this.TraceEvent(eventCache, source, eventType, id, string.Format(format, args));
}

public override void TraceEvent(TraceEventCache eventCache, string source, TraceEventType eventType, int id, string message)
{
string msg = BuildMessage(eventType, message);

//byte[] bytes = Encoding.GetEncoding(1251).GetBytes(sb.ToString().ToCharArray());
if (string.IsNullOrEmpty(base.LogFileName))
{
if (TraceConsole.TraceVerbose)
{
Console.Write(msg);
}
}
else
{
if (TraceLog.TraceVerbose)
{
File.AppendAllText(base.LogFileName, msg, Encoding.GetEncoding(1251));
}
}
}

private static string BuildMessage(TraceEventType eventType, string message)
{
StringBuilder sb = new StringBuilder();

//sb.AppendFormat("{0:dd.MM.yyyy HH:mm:ss.fff} ", DateTime.Now);
sb.AppendFormat("{0:dd.MM.yy HH:mm:ss} ", DateTime.Now);

//if (eventType != TraceEventType.Information)
//{
// sb.AppendFormat("{0} ", eventType); //Warning || Error
//}

//switch (eventType)
//{
// case TraceEventType.Verbose:
// sb.Append(" ");
// break;
// case TraceEventType.Information:
// sb.Append("i ");
// break;
// case TraceEventType.Warning:
// sb.Append("w ");
// break;
// case TraceEventType.Error:
// sb.Append("e ");
// break;
// case TraceEventType.Critical:
// sb.Append("E ");
// break;
//}

if (Trace.IndentLevel > 0)
{
sb.Append(" ".PadRight(Trace.IndentLevel * Trace.IndentSize));
}

return sb.AppendLine(message).ToString();
}
}
}
Loading

0 comments on commit ee5ec4d

Please sign in to comment.