Skip to content

Commit 7b65bdb

Browse files
authored
Panwang/addinglog (#508)
* adding windows event log * adding windows event log * fix a wrong bool variable * some updates * fix bad merge
1 parent 8707d71 commit 7b65bdb

22 files changed

+345
-689
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,8 @@ x64/
5252
src/AspNetCore/aspnetcore_msg.h
5353
src/AspNetCore/aspnetcore_msg.rc
5454
src/AspNetCore/version.h
55+
src/CommonLib/aspnetcore_msg.h
56+
src/CommonLib/aspnetcore_msg.rc
5557
.build
5658

5759
*.VC.*db

src/AspNetCore/AspNetCore.vcxproj

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,6 @@
202202
<ClInclude Include="Inc\applicationinfo.h" />
203203
<ClInclude Include="Inc\appoffline.h" />
204204
<ClInclude Include="inc\globalmodule.h" />
205-
<ClInclude Include="Inc\resource.h" />
206205
<ClInclude Include="Inc\applicationmanager.h" />
207206
<ClInclude Include="Inc\filewatcher.h" />
208207
<ClInclude Include="Inc\proxymodule.h" />

src/AspNetCore/Src/applicationinfo.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,9 +135,21 @@ APPLICATION_INFO::UpdateAppOfflineFileHandle()
135135
// recycle the application
136136
if (m_pApplication != NULL)
137137
{
138+
STACK_STRU(strEventMsg, 256);
139+
if (SUCCEEDED(strEventMsg.SafeSnwprintf(
140+
ASPNETCORE_EVENT_RECYCLE_APPOFFLINE_MSG,
141+
m_pApplication->QueryConfig()->QueryApplicationPath()->QueryStr())))
142+
{
143+
UTILITY::LogEvent(g_hEventLog,
144+
EVENTLOG_INFORMATION_TYPE,
145+
ASPNETCORE_EVENT_RECYCLE_APPOFFLINE,
146+
strEventMsg.QueryStr());
147+
}
148+
138149
m_pApplication->ShutDown();
139150
m_pApplication->DereferenceApplication();
140151
m_pApplication = NULL;
152+
141153
}
142154
}
143155
}

src/AspNetCore/Src/applicationmanager.cxx

Lines changed: 18 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ APPLICATION_MANAGER::GetApplicationInfo(
1919
BOOL fMixedHostingModelError = FALSE;
2020
BOOL fDuplicatedInProcessApp = FALSE;
2121
PCWSTR pszApplicationId = NULL;
22-
LPCWSTR apsz[1];
2322
STACK_STRU ( strEventMsg, 256 );
2423

2524
*ppApplicationInfo = NULL;
@@ -153,42 +152,24 @@ APPLICATION_MANAGER::GetApplicationInfo(
153152
ASPNETCORE_EVENT_DUPLICATED_INPROCESS_APP_MSG,
154153
pszApplicationId)))
155154
{
156-
/*apsz[0] = strEventMsg.QueryStr();
157-
if (FORWARDING_HANDLER::QueryEventLog() != NULL)
158-
{
159-
ReportEventW(FORWARDING_HANDLER::QueryEventLog(),
160-
EVENTLOG_ERROR_TYPE,
161-
0,
162-
ASPNETCORE_EVENT_DUPLICATED_INPROCESS_APP,
163-
NULL,
164-
1,
165-
0,
166-
apsz,
167-
NULL);
168-
}*/
155+
UTILITY::LogEvent(g_hEventLog,
156+
EVENTLOG_ERROR_TYPE,
157+
ASPNETCORE_EVENT_DUPLICATED_INPROCESS_APP,
158+
strEventMsg.QueryStr());
169159
}
170160
}
171161
else if (fMixedHostingModelError)
172162
{
173-
//if (SUCCEEDED(strEventMsg.SafeSnwprintf(
174-
// ASPNETCORE_EVENT_MIXED_HOSTING_MODEL_ERROR_MSG,
175-
// pszApplicationId,
176-
// pConfig->QueryHostingModelStr())))
177-
//{
178-
// apsz[0] = strEventMsg.QueryStr();
179-
// /*if (FORWARDING_HANDLER::QueryEventLog() != NULL)
180-
// {
181-
// ReportEventW(FORWARDING_HANDLER::QueryEventLog(),
182-
// EVENTLOG_ERROR_TYPE,
183-
// 0,
184-
// ASPNETCORE_EVENT_MIXED_HOSTING_MODEL_ERROR,
185-
// NULL,
186-
// 1,
187-
// 0,
188-
// apsz,
189-
// NULL);
190-
// }*/
191-
//}
163+
if (SUCCEEDED(strEventMsg.SafeSnwprintf(
164+
ASPNETCORE_EVENT_MIXED_HOSTING_MODEL_ERROR_MSG,
165+
pszApplicationId,
166+
pConfig->QueryHostingModel())))
167+
{
168+
UTILITY::LogEvent(g_hEventLog,
169+
EVENTLOG_ERROR_TYPE,
170+
ASPNETCORE_EVENT_MIXED_HOSTING_MODEL_ERROR,
171+
strEventMsg.QueryStr());
172+
}
192173
}
193174
else
194175
{
@@ -197,19 +178,10 @@ APPLICATION_MANAGER::GetApplicationInfo(
197178
pszApplicationId,
198179
hr)))
199180
{
200-
apsz[0] = strEventMsg.QueryStr();
201-
/*if (FORWARDING_HANDLER::QueryEventLog() != NULL)
202-
{
203-
ReportEventW(FORWARDING_HANDLER::QueryEventLog(),
204-
EVENTLOG_ERROR_TYPE,
205-
0,
206-
ASPNETCORE_EVENT_ADD_APPLICATION_ERROR,
207-
NULL,
208-
1,
209-
0,
210-
apsz,
211-
NULL);
212-
}*/
181+
UTILITY::LogEvent(g_hEventLog,
182+
EVENTLOG_ERROR_TYPE,
183+
ASPNETCORE_EVENT_ADD_APPLICATION_ERROR,
184+
strEventMsg.QueryStr());
213185
}
214186
}
215187
}

src/AspNetCore/Src/dllmain.cpp

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
HTTP_MODULE_ID g_pModuleId = NULL;
88
IHttpServer * g_pHttpServer = NULL;
9+
HANDLE g_hEventLog = NULL;
910
BOOL g_fRecycleProcessCalled = FALSE;
1011
PCWSTR g_pszModuleName = NULL;
1112
HINSTANCE g_hModule;
@@ -25,6 +26,11 @@ VOID
2526
StaticCleanup()
2627
{
2728
APPLICATION_MANAGER::Cleanup();
29+
if (g_hEventLog != NULL)
30+
{
31+
DeregisterEventSource(g_hEventLog);
32+
g_hEventLog = NULL;
33+
}
2834
}
2935

3036
BOOL WINAPI DllMain(HMODULE hModule,
@@ -100,6 +106,15 @@ HRESULT
100106
g_pszModuleName = pModuleInfo->GetName();
101107
g_pHttpServer = pHttpServer;
102108

109+
if (g_pHttpServer->IsCommandLineLaunch())
110+
{
111+
g_hEventLog = RegisterEventSource(NULL, ASPNETCORE_IISEXPRESS_EVENT_PROVIDER);
112+
}
113+
else
114+
{
115+
g_hEventLog = RegisterEventSource(NULL, ASPNETCORE_EVENT_PROVIDER);
116+
}
117+
103118
// check whether the feature is disabled due to security reason
104119
if (RegOpenKeyEx(HKEY_LOCAL_MACHINE,
105120
L"SOFTWARE\\Microsoft\\IIS Extensions\\IIS AspNetCore Module\\Parameters",
@@ -127,6 +142,17 @@ HRESULT
127142
if (fDisableANCM)
128143
{
129144
// Logging
145+
STACK_STRU(strEventMsg, 256);
146+
if (SUCCEEDED(strEventMsg.SafeSnwprintf(
147+
ASPNETCORE_EVENT_MODULE_DISABLED_MSG)))
148+
{
149+
UTILITY::LogEvent(g_hEventLog,
150+
EVENTLOG_WARNING_TYPE,
151+
ASPNETCORE_EVENT_MODULE_DISABLED,
152+
strEventMsg.QueryStr());
153+
}
154+
// this will return 500 error to client
155+
// as we did not register the module
130156
goto Finished;
131157
}
132158

src/AspNetCore/Src/precomp.hxx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -108,14 +108,14 @@ inline bool IsSpace(char ch)
108108
#include "..\..\CommonLib\utility.h"
109109
#include "..\..\CommonLib\debugutil.h"
110110
#include "..\..\CommonLib\requesthandler.h"
111-
//#include "..\aspnetcore_msg.h"
111+
#include "..\..\CommonLib\resources.h"
112+
#include "..\..\CommonLib\aspnetcore_msg.h"
112113
//#include "aspnetcore_event.h"
113114
#include "appoffline.h"
114115
#include "filewatcher.h"
115116
#include "applicationinfo.h"
116117
#include "applicationmanager.h"
117118
#include "globalmodule.h"
118-
#include "resource.h"
119119
#include "proxymodule.h"
120120
#include "applicationinfo.h"
121121

@@ -152,7 +152,7 @@ extern HINSTANCE g_hModule;
152152
extern HMODULE g_hAspnetCoreRH;
153153
extern SRWLOCK g_srwLock;
154154
extern PCWSTR g_pwzAspnetcoreRequestHandlerName;
155-
155+
extern HANDLE g_hEventLog;
156156
extern PFN_ASPNETCORE_CREATE_APPLICATION g_pfnAspNetCoreCreateApplication;
157157
extern PFN_ASPNETCORE_CREATE_REQUEST_HANDLER g_pfnAspNetCoreCreateRequestHandler;
158158
#pragma warning( error : 4091)

src/CommonLib/CommonLib.vcxproj

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,7 @@
177177
<ClInclude Include="fx_ver.h" />
178178
<ClInclude Include="hostfxr_utility.h" />
179179
<ClInclude Include="requesthandler.h" />
180+
<ClInclude Include="resources.h" />
180181
<ClInclude Include="stdafx.h" />
181182
<ClInclude Include="utility.h" />
182183
</ItemGroup>
@@ -199,6 +200,23 @@
199200
<Project>{4787a64f-9a3e-4867-a55a-70cb4b2b2ffe}</Project>
200201
</ProjectReference>
201202
</ItemGroup>
203+
<ItemGroup>
204+
<CustomBuild Include="aspnetcore_msg.mc">
205+
<FileType>Document</FileType>
206+
<Command Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">mc %(FullPath)</Command>
207+
<Message Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">Compiling Event Messages ...</Message>
208+
<Outputs Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">%(Filename).rc;%(Filename).h;MSG0409.bin</Outputs>
209+
<Command Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">mc %(FullPath)</Command>
210+
<Message Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">Compiling Event Messages ...</Message>
211+
<Outputs Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">%(Filename).rc;%(Filename).h;MSG0409.bin</Outputs>
212+
<Command Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">mc %(FullPath)</Command>
213+
<Message Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">Compiling Event Messages ...</Message>
214+
<Outputs Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">%(Filename).rc;%(Filename).h;MSG0409.bin</Outputs>
215+
<Command Condition="'$(Configuration)|$(Platform)'=='Release|x64'">mc %(FullPath)</Command>
216+
<Message Condition="'$(Configuration)|$(Platform)'=='Release|x64'">Compiling Event Messages ...</Message>
217+
<Outputs Condition="'$(Configuration)|$(Platform)'=='Release|x64'">%(Filename).rc;%(Filename).h;MSG0409.bin</Outputs>
218+
</CustomBuild>
219+
</ItemGroup>
202220
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
203221
<ImportGroup Label="ExtensionTargets">
204222
</ImportGroup>

src/RequestHandler/aspnetcore_msg.mc renamed to src/CommonLib/aspnetcore_msg.mc

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,18 @@ Language=English
9898
%1
9999
.
100100
101+
Messageid=1012
102+
SymbolicName=ASPNETCORE_EVENT_RECYCLE_APPOFFLINE
103+
Language=English
104+
%1
105+
.
106+
107+
Messageid=1013
108+
SymbolicName=ASPNETCORE_EVENT_MODULE_DISABLED
109+
Language=English
110+
%1
111+
.
112+
101113
;
102114
;#endif // _ASPNETCORE_MODULE_MSG_H_
103-
;
115+
;

src/RequestHandler/resource.h renamed to src/CommonLib/resources.h

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,19 +6,24 @@
66
#define IDS_INVALID_PROPERTY 1000
77
#define IDS_SERVER_ERROR 1001
88

9+
#define ASPNETCORE_EVENT_PROVIDER L"IIS AspNetCore Module"
10+
#define ASPNETCORE_IISEXPRESS_EVENT_PROVIDER L"IIS Express AspNetCore Module"
11+
912
#define ASPNETCORE_EVENT_MSG_BUFFER_SIZE 256
1013
#define ASPNETCORE_EVENT_PROCESS_START_SUCCESS_MSG L"Application '%s' started process '%d' successfully and is listening on port '%d'."
1114
#define ASPNETCORE_EVENT_RAPID_FAIL_COUNT_EXCEEDED_MSG L"Maximum rapid fail count per minute of '%d' exceeded."
1215
#define ASPNETCORE_EVENT_PROCESS_START_INTERNAL_ERROR_MSG L"Application '%s' failed to parse processPath and arguments due to internal error, ErrorCode = '0x%x'."
13-
#define ASPNETCORE_EVENT_PROCESS_START_POSTCREATE_ERROR_MSG L"Application '%s' with physical root '%s' created process with commandline '%s'but failed to get its status, ErrorCode = '0x%x'."
14-
#define ASPNETCORE_EVENT_PROCESS_START_ERROR_MSG L"Application '%s' with physical root '%s' failed to start process with commandline '%s', ErrorCode = '0x%x' processStatus code '%x'."
16+
#define ASPNETCORE_EVENT_PROCESS_START_POSTCREATE_ERROR_MSG L"Application '%s' with physical root '%s' created process with commandline '%s'but failed to get its status, ErrorCode = '0x%x', retryCounter '%d'."
17+
#define ASPNETCORE_EVENT_PROCESS_START_ERROR_MSG L"Application '%s' with physical root '%s' failed to start process with commandline '%s', ErrorCode = '0x%x', retryCounter '%d'."
1518
#define ASPNETCORE_EVENT_PROCESS_START_WRONGPORT_ERROR_MSG L"Application '%s' with physical root '%s' created process with commandline '%s' but failed to listen on the given port '%d'"
1619
#define ASPNETCORE_EVENT_PROCESS_START_NOTREADY_ERROR_MSG L"Application '%s' with physical root '%s' created process with commandline '%s' but either crashed or did not reponse or did not listen on the given port '%d', ErrorCode = '0x%x'"
1720
#define ASPNETCORE_EVENT_INVALID_STDOUT_LOG_FILE_MSG L"Warning: Could not create stdoutLogFile %s, ErrorCode = %d."
1821
#define ASPNETCORE_EVENT_GRACEFUL_SHUTDOWN_FAILURE_MSG L"Failed to gracefully shutdown process '%d'."
1922
#define ASPNETCORE_EVENT_SENT_SHUTDOWN_HTTP_REQUEST_MSG L"Sent shutdown HTTP message to process '%d' and received http status '%d'."
2023
#define ASPNETCORE_EVENT_LOAD_CLR_FALIURE_MSG L"Application '%s' with physical root '%s' failed to load clr and managed application, ErrorCode = '0x%x."
2124
#define ASPNETCORE_EVENT_DUPLICATED_INPROCESS_APP_MSG L"Only one inprocess application is allowed per IIS application pool. Please assign the application '%s' to a different IIS application pool."
22-
#define ASPNETCORE_EVENT_MIXED_HOSTING_MODEL_ERROR_MSG L"Mixed hosting model is not supported. Application '%s' configured with different hostingModel value '%s' other than the one of running application(s)."
25+
#define ASPNETCORE_EVENT_MIXED_HOSTING_MODEL_ERROR_MSG L"Mixed hosting model is not supported. Application '%s' configured with different hostingModel value '%d' other than the one of running application(s)."
2326
#define ASPNETCORE_EVENT_ADD_APPLICATION_ERROR_MSG L"Failed to start application '%s', ErrorCode '0x%x'."
24-
#define ASPNETCORE_EVENT_INPROCESS_THREAD_EXIT_MSG L"Application '%s' with physical root '%s' hit unexpected managed background thread exit, ErrorCode = '0x%x."
27+
#define ASPNETCORE_EVENT_INPROCESS_THREAD_EXIT_MSG L"Application '%s' with physical root '%s' hit unexpected managed background thread eixt, ErrorCode = '0x%x."
28+
#define ASPNETCORE_EVENT_RECYCLE_APPOFFLINE_MSG L"Application '%s' is recycled due to app_offline file was detected."
29+
#define ASPNETCORE_EVENT_MODULE_DISABLED_MSG L"AspNetCore Module is disabled"

src/CommonLib/utility.cxx

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -599,3 +599,31 @@ UTILITY::CheckIfFileExists(
599599

600600
return fFileExists;
601601
}
602+
603+
VOID
604+
UTILITY::LogEvent(
605+
_In_ HANDLE hEventLog,
606+
_In_ WORD dwEventInfoType,
607+
_In_ DWORD dwEventId,
608+
_In_ LPCWSTR pstrMsg
609+
)
610+
{
611+
if (hEventLog != NULL)
612+
{
613+
ReportEventW(hEventLog,
614+
dwEventInfoType,
615+
0, // wCategory
616+
dwEventId,
617+
NULL, // lpUserSid
618+
1, // wNumStrings
619+
0, // dwDataSize,
620+
&pstrMsg,
621+
NULL // lpRawData
622+
);
623+
}
624+
625+
if (dwEventInfoType == EVENTLOG_ERROR_TYPE)
626+
{
627+
fwprintf(stderr, L"ERROR: %s\n", pstrMsg);
628+
}
629+
}

src/CommonLib/utility.h

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,8 +111,17 @@ class UTILITY
111111
PCWSTR pszFilePath
112112
);
113113

114+
static
115+
VOID
116+
LogEvent(
117+
_In_ HANDLE hEventLog,
118+
_In_ WORD dwEventInfoType,
119+
_In_ DWORD dwEventId,
120+
_In_ LPCWSTR pstrMsg
121+
);
122+
114123
private:
115124

116125
UTILITY() {}
117126
~UTILITY() {}
118-
};
127+
};

src/RequestHandler/RequestHandler.rc

-2.6 KB
Binary file not shown.

src/RequestHandler/RequestHandler.vcxproj

Lines changed: 1 addition & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,7 @@
201201
</ItemDefinitionGroup>
202202
<ItemGroup>
203203
<ClInclude Include="aspnetcore_event.h" />
204-
<ClInclude Include="aspnetcore_msg.h" />
204+
<ClInclude Include="disconnectcontext.h" />
205205
<ClInclude Include="sttimer.h" />
206206
<ClInclude Include="outofprocess\forwarderconnection.h" />
207207
<ClInclude Include="outofprocess\processmanager.h" />
@@ -211,7 +211,6 @@
211211
<ClInclude Include="outofprocess\websockethandler.h" />
212212
<ClInclude Include="outofprocess\winhttphelper.h" />
213213
<ClInclude Include="precomp.hxx" />
214-
<ClInclude Include="resource.h" />
215214
<ClInclude Include=".\inprocess\inprocessapplication.h" />
216215
<ClInclude Include=".\inprocess\inprocesshandler.h" />
217216
<ClInclude Include=".\outofprocess\forwardinghandler.h" />
@@ -232,23 +231,6 @@
232231
<ClCompile Include="outofprocess\websockethandler.cxx" />
233232
<ClCompile Include="outofprocess\winhttphelper.cxx" />
234233
</ItemGroup>
235-
<ItemGroup>
236-
<CustomBuild Include="aspnetcore_msg.mc">
237-
<FileType>Document</FileType>
238-
<Command Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">mc %(FullPath)</Command>
239-
<Message Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">Compiling Event Messages ...</Message>
240-
<Outputs Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">%(Filename).rc;%(Filename).h;MSG0409.bin</Outputs>
241-
<Command Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">mc %(FullPath)</Command>
242-
<Message Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">Compiling Event Messages ...</Message>
243-
<Outputs Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">%(Filename).rc;%(Filename).h;MSG0409.bin</Outputs>
244-
<Command Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">mc %(FullPath)</Command>
245-
<Message Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">Compiling Event Messages ...</Message>
246-
<Outputs Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">%(Filename).rc;%(Filename).h;MSG0409.bin</Outputs>
247-
<Command Condition="'$(Configuration)|$(Platform)'=='Release|x64'">mc %(FullPath)</Command>
248-
<Message Condition="'$(Configuration)|$(Platform)'=='Release|x64'">Compiling Event Messages ...</Message>
249-
<Outputs Condition="'$(Configuration)|$(Platform)'=='Release|x64'">%(Filename).rc;%(Filename).h;MSG0409.bin</Outputs>
250-
</CustomBuild>
251-
</ItemGroup>
252234
<ItemGroup>
253235
<ProjectReference Include="..\CommonLib\CommonLib.vcxproj">
254236
<Project>{55494e58-e061-4c4c-a0a8-837008e72f85}</Project>
@@ -257,9 +239,6 @@
257239
<Project>{4787a64f-9a3e-4867-a55a-70cb4b2b2ffe}</Project>
258240
</ProjectReference>
259241
</ItemGroup>
260-
<ItemGroup>
261-
<ResourceCompile Include="RequestHandler.rc" />
262-
</ItemGroup>
263242
<ItemGroup>
264243
<None Include="Source.def" />
265244
</ItemGroup>

0 commit comments

Comments
 (0)