Skip to content

Commit

Permalink
Dependency update and code cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
MattMofDoom committed Jan 24, 2022
1 parent 82fe4e5 commit cfe043c
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 59 deletions.
2 changes: 1 addition & 1 deletion MailboxReporter/Classes/Config.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ static Config()
Password = ConfigurationManager.AppSettings["Password"];
Url = ConfigurationManager.AppSettings["Url"];
foreach (var mailbox in (ConfigurationManager.AppSettings["Addresses"] ?? "")
.Split(new[] {','}, StringSplitOptions.RemoveEmptyEntries).Select(t => t.Trim()).ToList())
.Split(new[] {','}, StringSplitOptions.RemoveEmptyEntries).Select(t => t.Trim()).ToList())
Addresses.Add(new MailboxSetting
{Address = mailbox, NextInterval = DateTime.Now.AddSeconds(5)});
UseGzip = GetBool(ConfigurationManager.AppSettings["UseGzip"]);
Expand Down
67 changes: 34 additions & 33 deletions MailboxReporter/Classes/Emails.cs
Original file line number Diff line number Diff line change
Expand Up @@ -122,39 +122,40 @@ public static EmailResult ListEmails(string address)
address, pageCount, fiResults.Items.Count);

foreach (var emailRecord in fiResults.Items
.Select(mailItem => EmailMessage.Bind(exchangeServer, mailItem.Id,
new PropertySet(BasePropertySet.FirstClassProperties)))
.Select(email => new EmailRecord
{
Id = email.Id.UniqueId,
ConversationId = email.ConversationId.UniqueId,
InternetMessageId = email.InternetMessageId,
SentDate = email.DateTimeCreated,
ReceivedDate = email.DateTimeReceived,
CreatedDate = email.DateTimeCreated,
ModifiedDate = email.LastModifiedTime,
ModifiedName = email.LastModifiedName,
BodyType = email.Body.BodyType,
Body = Config.IncludePartialBody && !string.IsNullOrEmpty(email.Body)
? email.Body.Text.Length < Config.PartialBodyLength ? email.Body.Text :
email.Body.Text.Substring(0, Config.PartialBodyLength)
: "",
Subject = email.Subject,
FromName = email.Sender.Name,
FromAddress = email.Sender.Address,
ReplyToName = string.Join(",", email.ReplyTo.Select(x => x.Name).ToArray()),
ReplyToAddress = string.Join(",", email.ReplyTo.Select(x => x.Address).ToArray()),
ToName = string.Join(",", email.ToRecipients.Select(x => x.Name).ToArray()),
ToAddress = string.Join(",", email.ToRecipients.Select(x => x.Address).ToArray()),
CcName = string.Join(",", email.CcRecipients.Select(x => x.Name).ToArray()),
CcAddress = string.Join(",", email.CcRecipients.Select(x => x.Address).ToArray()),
IsRead = email.IsRead,
Priority = email.Importance,
AttachmentCount = email.Attachments.Where(x => !x.IsInline).Select(x => x.Name).Count(),
Attachments = string.Join(",",
email.Attachments.Where(x => !x.IsInline).Select(x => x.Name).ToArray()),
Size = email.Size
}))
.Select(mailItem => EmailMessage.Bind(exchangeServer, mailItem.Id,
new PropertySet(BasePropertySet.FirstClassProperties)))
.Select(email => new EmailRecord
{
Id = email.Id.UniqueId,
ConversationId = email.ConversationId.UniqueId,
InternetMessageId = email.InternetMessageId,
SentDate = email.DateTimeCreated,
ReceivedDate = email.DateTimeReceived,
CreatedDate = email.DateTimeCreated,
ModifiedDate = email.LastModifiedTime,
ModifiedName = email.LastModifiedName,
BodyType = email.Body.BodyType,
Body = Config.IncludePartialBody && !string.IsNullOrEmpty(email.Body)
? email.Body.Text.Length < Config.PartialBodyLength ? email.Body.Text :
email.Body.Text.Substring(0, Config.PartialBodyLength)
: "",
Subject = email.Subject,
FromName = email.Sender.Name,
FromAddress = email.Sender.Address,
ReplyToName = string.Join(",", email.ReplyTo.Select(x => x.Name).ToArray()),
ReplyToAddress = string.Join(",", email.ReplyTo.Select(x => x.Address).ToArray()),
ToName = string.Join(",", email.ToRecipients.Select(x => x.Name).ToArray()),
ToAddress = string.Join(",", email.ToRecipients.Select(x => x.Address).ToArray()),
CcName = string.Join(",", email.CcRecipients.Select(x => x.Name).ToArray()),
CcAddress = string.Join(",", email.CcRecipients.Select(x => x.Address).ToArray()),
IsRead = email.IsRead,
Priority = email.Importance,
AttachmentCount =
email.Attachments.Where(x => !x.IsInline).Select(x => x.Name).Count(),
Attachments = string.Join(",",
email.Attachments.Where(x => !x.IsInline).Select(x => x.Name).ToArray()),
Size = email.Size
}))
{
sqlClient.AddOrUpdate(address, emailRecord);
totalCount++;
Expand Down
14 changes: 7 additions & 7 deletions MailboxReporter/Classes/SqlServerClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,39 +7,39 @@ namespace MailboxReporter.Classes
{
public class SqlServerClient : IDisposable
{
private readonly SqlConnection _reportConnection =
private readonly SqlConnection reportConnection =
new SqlConnection(ConfigurationManager.ConnectionStrings["MailboxReporterConnection"].ToString());

public void Dispose()
{
if (IsOpen())
Disconnect();

_reportConnection?.Dispose();
reportConnection?.Dispose();
}

public void Connect()
{
_reportConnection.Open();
reportConnection.Open();
}

public void Disconnect()
{
_reportConnection.Close();
reportConnection.Close();
}

private bool IsOpen()
{
return _reportConnection != null && _reportConnection.State > ConnectionState.Closed &&
!_reportConnection.State.Equals(ConnectionState.Broken);
return reportConnection != null && reportConnection.State > ConnectionState.Closed &&
!reportConnection.State.Equals(ConnectionState.Broken);
}

public void AddOrUpdate(string mailboxAddress, EmailRecord email)
{
if (!IsOpen())
Connect();

var sqlCmd = new SqlCommand("MailboxReporter_AddOrUpdate", _reportConnection)
var sqlCmd = new SqlCommand("MailboxReporter_AddOrUpdate", reportConnection)
{
CommandType = CommandType.StoredProcedure,
Parameters =
Expand Down
4 changes: 2 additions & 2 deletions MailboxReporter/MailboxReporter.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,8 @@
<Folder Include="Properties\" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="Lurgle.Logging" Version="1.2.8" />
<PackageReference Include="Microsoft.Data.SqlClient" Version="4.0.0" />
<PackageReference Include="Lurgle.Logging" Version="1.2.9" />
<PackageReference Include="Microsoft.Data.SqlClient" Version="4.0.1" />
<PackageReference Include="Microsoft.Exchange.WebServices" Version="2.2.0" />
</ItemGroup>
</Project>
32 changes: 16 additions & 16 deletions MailboxReporter/Reporter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ public partial class Reporter : ServiceBase
{
private const int Available = 0;
private const int Locked = 1;
private int _lockState;
private Timer _reportTimer;
private int lockState;
private Timer reportTimer;

public Reporter()
{
Expand Down Expand Up @@ -58,14 +58,14 @@ protected override void OnStart(string[] args)
Log.Debug().Add("Last tick: {LastTick:l}", Config.LastTick.ToString("dd MMM yyyy HH:mm:ss"));
}

_reportTimer = new Timer {Interval = 1000, AutoReset = false};
_reportTimer.Elapsed += ReportTick;
_reportTimer.Start();
reportTimer = new Timer {Interval = 1000, AutoReset = false};
reportTimer.Elapsed += ReportTick;
reportTimer.Start();
}

protected override void OnStop()
{
_reportTimer.Stop();
reportTimer.Stop();
Log.Information().Add("{Service:l} v{Version:l} stopped (Exit Code: {ExitCode})",
Logging.Config.AppName, Logging.Config.AppVersion, AppErrors.Success);
Logging.Close();
Expand All @@ -77,13 +77,13 @@ private void ReportTick(object sender, EventArgs e)

try
{
if (Interlocked.CompareExchange(ref _lockState, Locked, Available) != Available) return;
if (Interlocked.CompareExchange(ref lockState, Locked, Available) != Available) return;

if (_reportTimer != null && !_reportTimer.AutoReset)
if (reportTimer != null && !reportTimer.AutoReset)
{
_reportTimer.Interval = 1000;
_reportTimer.AutoReset = true;
_reportTimer.Start();
reportTimer.Interval = 1000;
reportTimer.AutoReset = true;
reportTimer.Start();
}

var thisReportTick = DateTime.Now;
Expand All @@ -101,16 +101,16 @@ private void ReportTick(object sender, EventArgs e)
try
{
currentAddress = thread.Address;
var result = Emails.ListEmails(thread.Address);
var unused = Emails.ListEmails(thread.Address);
foreach (var mailbox in Config.Addresses.Where(mailbox =>
thread.Address == mailbox.Address))
thread.Address == mailbox.Address))
mailbox.NextInterval = DateTime.Now.AddMilliseconds(Config.PollInterval);
}
catch (Exception ex)
{
isError = true;
foreach (var mailbox in Config.Addresses.Where(mailbox =>
currentAddress == mailbox.Address))
currentAddress == mailbox.Address))
mailbox.NextInterval = DateTime.Now.AddMilliseconds(Config.BackoffInterval);

Log.Warning()
Expand Down Expand Up @@ -144,7 +144,7 @@ private void ReportTick(object sender, EventArgs e)
ex.Message);
}

Interlocked.CompareExchange(ref _lockState, Available, Locked);
Interlocked.CompareExchange(ref lockState, Available, Locked);
}

private void UnhandledException(object sender, UnhandledExceptionEventArgs e)
Expand All @@ -158,7 +158,7 @@ private void UnhandledException(object sender, UnhandledExceptionEventArgs e)

private void StopError(AppErrors errorType)
{
_reportTimer.Stop();
reportTimer.Stop();
Log.Information().Add("{Service:l} v{Version:l} stopped (Exit Code: {ExitCode})",
Logging.Config.AppName, Logging.Config.AppVersion, errorType);
Logging.Close();
Expand Down

0 comments on commit cfe043c

Please sign in to comment.